Forum Moderators: coopster
Any suggestions on how to improve this would be welcome, especially a) ways of making it handle nested tags sensibly and b) any ideas on how to pass further parameters into the functions.
<?PHP
function functions($matches)
{
if (function_exists($matches[1])) return call_user_func($matches[1],$matches[2]);
else return $matches[0];
}
function wordreverse($contents)
{
$words=split(" ",$contents);
return join(" ",array_reverse($words));
}
$line="<strrev>This is a test</strrev>. <br/>
<wordreverse>Reverse this list of <b>words</b></wordreverse>.<br/>
<b>This</b> is <strtoupper>also</strtoupper> a test.";
$pattern="/<([A-Z][A-Z0-9]*)>(.*?)<\/\\1>/i";
$line=preg_replace_callback($pattern,"functions",$line);
echo($line);
?>
Variable Scope [php.net]