Hi all, I'm pretty clueless when it comes to PHP and regex but I'm trying to fix a broken plugin for my forum.
I'd like to replace the following:
<blockquote rel="blah">foo</blockquote>
With
<blockquote class="a"><div class="b">blah</div><div class="c"><p>foo</p></div></blockquote>
Actually, that part is easy and I've already partially fixed the plugin to do this. The following regex is being used in a call to preg_replace_callback()
to do the replacement:
/(<blockquote rel="([\d\w_ ]{3,30})">)(.*)(<\/blockquote>)/u
The callback code is:
return <<<BLOCKQUOTE
<blockquote class="a"><div class="b">{$Matches[2]}</div><div class="c"><p>{$Matches[3]}</p></div></blockquote>
BLOCKQUOTE;
And that works for my above example (non-nested blockquotes). However, if the blockquotes are nested, such as in the following example:
<blockquote rel="blah">foo <blockquote rel="bloop">bar ...maybe another nest...</blockquote></blockquote>
It doesn't work. So my question is, how can I replace all nested blockquotes using a combination of regex/PHP? I know recursive patterns are possible in PHP with (?R)
; the following regex will extract all nested blockquotes from the string containing them:
/(<blockquote rel="([\d\w_ ]{3,30})">)(.*|(?R))(<\/blockquote>)/s
But from there on I'm not quite sure what to do in the preg_replace_callback()
callback to replace each nested blockquote with the above replacement.
Any help would be appreciated.