0

I have been working on this for whole day but couldn't find a solution in which I can replace substrings in string in php like I have string

'<div>
   <h2>this is <span>String</span> found in h2 tag</h2>
   <p>Hello World</p>
   <h2>this is <span>String</span> found in h2 tag</h2>
   <p>Hello Universe</p>
   <h2>this is <span>String</span> found in h2 tag</h2>
</div>'

I want to get every string inside h2 and then perform some htmlentity replacement like

$str = 'this is <span>String</span> found in h2 tag';
$sanitized = htmlspecialchars($str,ENT_QUOTES);

and then output complete string but replaced.

How it can be done?

<div>
    <h2>this is &lt;span&gt;String&lt;/span&gt; found in h2 tag</h2>
    <p><b>Hello</b> World</p>
    <h3>this is <span>String</span> found in h2 tag</h3>
    <p><b>hello</b> Universe</p>
    <h2>this is &lt;span&gt;String&lt;/span&gt; found in h2 tag</h2>
</div>
4
  • See if this helps us2.php.net/book.dom Commented Feb 15, 2014 at 23:24
  • cannot there be some simple solution to get substring and perform some calculations and then replace? This seems I will have to rework alot. And truth is that I have become dumb now as I have already invested my whole day on it Commented Feb 15, 2014 at 23:27
  • 2
    I would point you to this question stackoverflow.com/questions/1732348/…. Commented Feb 15, 2014 at 23:31
  • 2
    While it might seem simpler to use substr, dom is definitely the right solution. Embrace the lost day as a learning experience. Commented Feb 15, 2014 at 23:41

1 Answer 1

3

You can use preg_replace_callback(). Regular expression for matching <h2> tags is :

/<h2>(.+?)<\/h2>/  

If you would like to match all <hx>tags instead use the following instead:

/<h([1-6])(.*?)<\/h\1>/ 

In callback function you can alter the matched string. For example:

$html = <<< EOH

<div>
   <h2>this is <span>String</span> found in h2 tag</h2>
   <p>Hello World</p>
   <h2>this is <span>String</span> found in h2 tag</h2>
   <p>Hello Universe</p>
   <h2>this is <span>String</span> found in h2 tag</h2>
</div>

EOH;

$html = preg_replace_callback("/<h2>(.+?)<\/h2>/", function($matches) {
    /* Convert content of <h2> tags to HTML entities. */
    $altered =  htmlspecialchars($matches[1], ENT_QUOTES);

    /* Put the converted content back inside <h2> tag and return it. */
    return str_replace($matches[1], $altered, $matches[0]);
}, $html);

$html = preg_replace_callback("/<p>(.+?)<\/p>/", function($matches) {
    /* Make match bold. */
    $altered = "<b>" . $matches[1] . "</b>";

    /* Put the converted content back inside <p> tag and return it. */
    return str_replace($matches[1], $altered, $matches[0]);
}, $html);

print $html;

Output of the above script is:

<div>
   <h2>this is &lt;span&gt;String&lt;/span&gt; found in h2 tag</h2>
   <p><b>Hello World</b></p>
   <h2>this is &lt;span&gt;String&lt;/span&gt; found in h2 tag</h2>
   <p><b>Hello Universe</b></p>
   <h2>this is &lt;span&gt;String&lt;/span&gt; found in h2 tag</h2>
</div>
Sign up to request clarification or add additional context in comments.

7 Comments

WOW Mika this is just awesome man. Can I replace <p> content as well altogether with <h2> to change Hello in <p> as well? I am editing my question, please see if it possible
I want to make bold all of <p> tags as well beside replacing h2 contents. Is it possible to do it in same function you said?
Updated the example to match additional question. If it answers the question mark it approved.
Thanks Mika. Isn't it possible to do both of bold and htmlreplacement in one single function?
You can pass array of regexps to preg_replace_callback() so it could be done. Problem is you need to also know which regexp watch matched since you are doing different transformations on them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.