preg_replace 正在将函数作为属性处理

发布于 2024-10-05 10:07:43 字数 507 浏览 2 评论 0原文

我正在尝试为我正在开发的网站创建一个模板系统。我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2024-10-12 10:07:43

我建议使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文