开发者

preg_replace is processing a function as a property

开发者 https://www.devze.com 2023-01-28 04:47 出处:网络
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(\'da

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号