Passing regexp substrings to a function
| From: | Ville Mattila | Date: | Mon, 25 Oct 2004 12:33:59 +0000 |
| Subject: | Passing regexp substrings to a function | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-200237@lists.php.net to get a copy of this message | ||
Hi there,
I have a few e-mail templates in a file that should be parsed. A template can include also some "module codes" that should be replaced by a return value of a certain function. For example, if the template include a text "{ProductInfo:1032}", the value 1032 would be passed as an argument to a function mdlProductInfo() and the whole string would be replaced by the return value of the function.
One possible way is to loop the string like this:
while(eregi("{([a-z])(:[a-z0-9])+}", $template, $regs)) {
list($whole, $function, $valuestring) = $regs;
$values = explode(":", substr($valuestring, 1));
unset($retval);
eval("$retval = mdl".$function."(".join(",", $values).");");
$template = str_replace($whole, $retval, $template);
}
Any other ideas on this?
Ville