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 <span>String</span> 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 <span>String</span> found in h2 tag</h2>
</div>