Warning: Undefined array key "wp:featuredmedia" in /home/rajkar/public_html/blogsdir/blog-details.php on line 38

Warning: Trying to access array offset on value of type null in /home/rajkar/public_html/blogsdir/blog-details.php on line 38

Warning: Trying to access array offset on value of type null in /home/rajkar/public_html/blogsdir/blog-details.php on line 38

Warning: Trying to access array offset on value of type null in /home/rajkar/public_html/blogsdir/blog-details.php on line 38

Warning: Undefined array key "wp:featuredmedia" in /home/rajkar/public_html/blogsdir/blog-details.php on line 39

Warning: Trying to access array offset on value of type null in /home/rajkar/public_html/blogsdir/blog-details.php on line 39

Warning: Trying to access array offset on value of type null in /home/rajkar/public_html/blogsdir/blog-details.php on line 39

Warning: Trying to access array offset on value of type null in /home/rajkar/public_html/blogsdir/blog-details.php on line 39

Warning: Undefined array key "wp:featuredmedia" in /home/rajkar/public_html/blogsdir/blog-details.php on line 40

Warning: Trying to access array offset on value of type null in /home/rajkar/public_html/blogsdir/blog-details.php on line 40

Warning: Trying to access array offset on value of type null in /home/rajkar/public_html/blogsdir/blog-details.php on line 40

Warning: Trying to access array offset on value of type null in /home/rajkar/public_html/blogsdir/blog-details.php on line 40
Replace any text from html text by tag name with specific attribute in php - Rajkar Global CMS - Rajkar Global Consultancy Private Limited
Blog Image

Replace any text from html text by tag name with specific attribute in php

function replace_tag_with_specific_attr_type( $tagAttribute, $tagAttributeValue, $subjectStr, $tagName=null,$replaceStr ) {
if( is_null($tagName) )
$tagName = ‘\w+’;
else
$tagName = preg_quote($tagName);

$tagAttribute = preg_quote($tagAttribute);
$tagAttributeValue = preg_quote($tagAttributeValue);

$match_regex = “/<(“.$tagName.”)[^>]*$tagAttribute\s*=\s*”.
“([‘\”])$tagAttributeValue\\2[^>]*>(.*?)<\/\\1>/”;

preg_match_all($match_regex,
$subjectStr,
$matches,
PREG_PATTERN_ORDER);

$result = str_replace($matches[3],$replaceStr,$subjectStr);
return $result;
}

Usage :
Say for Example

$ActualStr : ‘<div class=’bannerpicsd’ ><span style=”margin: 0px 0px 0px 0px; width: 540px;”>something</span><p style=”float: right; margin: -240px 0 0; width: 200px;”>This is the actual String</p></div>’;
$StrToReplaceWith = ‘This is the replaces str’;

Now you want to replace all the text inside the div tags with class name “bannerpicsd”  from $ActualStr with $StrToReplaceWith
Then you can do as below:

$replacedStr  = replace_tag_with_specific_attr_type(‘class’,’bannerpicsd’,$ActualStr,’div’,$StrToReplaceWith);
Result :
<div class=”bannerpicsd” >This is the replaces str</div>
Enjoy 🙂



Author: admin

Vinod Ram has been in Software Industry since 2006 and has experience of over 16 years in Software Development & Project Management domain specialised majorly in LAMP stack & Open Source Technology, building enterprise level Web based Application, Large Database driven and huge traffic Websites and Project Management. He loves to write information articles and blog to share his knowledge and experience with the outside world and help people to find solution for their problems.