preg_replace 正在将函数作为属性处理
我正在尝试为我正在开发的网站创建一个模板系统。我正在尝试使用 preg_replace 调用函数来处理由某些标签包围的数据。例如,process('date') 将返回今天的日期,并以 $date$ 的形式放入代码中。我正在使用 preg_replace 来查找我需要修改的字符串,但我无法调用该函数 - PHP 似乎认为该函数是一个属性。
这是我正在使用的代码:
$view = preg_replace("/\$_([a-zA-Z0-9_]+)_\$/e", "$this->process('\\1')", $view);
这是我得到的错误:
Notice: Undefined property: ClassName::$process in /location/ClassName.class.php on line X
我希望我对此进行了充分的解释。如何让 preg_replace 作为方法执行进程,而不是尝试将其显示为属性?
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", "$this->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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 preg_replace_callback 而不是 preg_replace 视为 preg_replace_callback是为您实际想做的事情而设计的。您可能遇到的一个问题是范围/函数的可访问性。如果调用 preg_replace 的代码无法访问您尝试调用的函数,那么您可能会收到此错误,因为 PHP 找不到该函数并且可能默认将其视为参数。如果该函数与此 preg_replace 不在同一文件中,请尝试将其放在那里以查看是否会更改任何内容。
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.