Forum Moderators: coopster

Message Too Old, No Replies

PHP functions in an xml string

A way of including php functions in an xml string...

         

hollow

10:15 am on Jun 18, 2004 (gmt 0)

10+ Year Member



This might be of interest, something i've just been messing about with which allows you to include php string functions in xml.

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);
?>

coopster

2:45 pm on Jun 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Also remember that you don't always have to pass parameters into a function. The global keyword will allow you to use any variable inside the function.

Variable Scope [php.net]