PHP regex help -- reverse search?

Posted by Ian Silber on Stack Overflow See other posts from Stack Overflow or by Ian Silber
Published on 2010-04-09T21:09:41Z Indexed on 2010/04/09 21:13 UTC
Read the original article Hit count: 302

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about regex

Related posts about php