|
Posted by Michael on 02/04/07 15:36
I found this solution:
function ReplaceTags($input) {
$offset = 0;
$regex = '#\[(.*?)((?:\|.*?)*)(?:/|\](.*?)\[/\1)\]#s';
// Find a matching tag pair, using offset $offset
while (preg_match($regex, $input, $matches, PREG_OFFSET_CAPTURE,
$offset)) {
// If it contains another opening tag, skip over this and let
// the next loop execution handle it
if (preg_match('#\[[^/](.*?)\]#s', $matches[3][0], $m))
$offset = $matches[0][1] + 1;
else
{
// If it's not an opening tag, we can safely parse it
// Replace the tag with it's processed value and start from the beginning
$input = substr_replace($input, ParseTag(array($matches[0][0],
$matches[1][0], $matches[2][0], $matches[3][0])), $matches[0][1],
strlen($matches[0][0]));
$offset = 0;
}
}
return $input;
}
Don't mind the ParseTag call, it's the old version expecting an array
directly from the preg_match. Doesn't seem to be too slow - I expect to be
parsing mostly top-level and singly nested tags anyway.
Kind regards
Michael.
[Back to original message]
|