I am trying to create a template system for a web site I am working on. I am trying to use preg_replace to call a function to process data that is surrounded by certain tags. For example, process('date') would return today's date, and is put in the code as $date$. I am using preg_replace to find the strings I need to modify, but I can not call the function - PHP seems to think that the function is a property instead.
This is the code I am using:
$view = preg_replace("/\$_([a-zA-Z0-9_]+)_\$/e", "$thi开发者_JAVA技巧s->process('\\1')", $view);
This is the error I get:
Notice: Undefined property: ClassName::$process in /location/ClassName.class.php on line X
I hope I explained this sufficiently. How do I get preg_replace to execute process as a method, instead of trying to display it as a property?
I would suggest using preg_replace_callback instead of preg_replace seeing as preg_replace_callback is designed for what you are actually trying to do. A possible issue you could be having is scope/function accessibility. If your code that is calling preg_replace doesn't have access to the function you are trying to call, then you could be getting this error since PHP can't find the function and might be defaulting to treating it as an parameter. If the function isn't in the same file as this preg_replace, try putting it there to see if it changes anything.
精彩评论