tags:

views:

188

answers:

2

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 <tag> with something special, say for example, <end_tag>.

Any ideas?

A: 

If I read this right, you want to find the last closing tag in the document.

You could find the last occurrence of </*> which has no more '<>' characters after it. This will be the last tag, assuming all remaining angle-brackets are encoded as &lt; and &gt;:

<?php
$html = '<div id="test"><p style="hello_world">This is a test.</p></div>';

// Outputs:
// '<div id="test"><p style="hello_world">This is a test.</p></tag>'
echo preg_replace('/<\/[A-Z][A-Z0-9]*>([^<>]*)$/i', '</tag>$1', $html);

This will replace the final </div> with </tag>, preserving any content that follows the final closing tag.

I don't know why you'd want to do this with only the closing tag, as if you change it you also have to change the matching opening tag. Also, this will fail to find the last self-closing tag, like <img /> or <br />.

meagar
A: 

I believe this method works the same as @meager's, but is more concise:

<?php
$html = '<div id="test"><p style="hello_world">This is a test.</p></div>';
$readmore = ' <a href="/foo/bar">Read More&hellip;</a>';

// Outputs:
// '<div id="test"><p style="hello_world">This is a test.</p> <a href="/foo/bar">Read More&hellip;</a></div>'
echo preg_replace('#</\w>\s*$#', $readmore .'$1', $html);
?>
Dalin