PHP regex help -- reverse search?
- by Ian Silber
So, I have a regex that searches for HTML tags and modifies them slightly. It's working great, but I need to do something special with the last closing HTML tag I find. Not sure of the best way to do this. I'm thinking some sort of reverse reg ex, but haven't found a way to do that. Here's my code so far:
$html = "<div id="test"><p style="hello_world">This is a test.</p></div>";
$pattern = array('/<([A-Z][A-Z0-9]*)(\b[^>]*)>/i');
$replace = array('<tag>');
$html = preg_replace($pattern,$replace,$html);
// Outputs: <tag><tag>This is a test</p></div>
I'd like to replace the last occurance of "" with something special, say for example, "".
Any ideas?