preg_replace 反向引用日期函数

发布于 2024-12-26 07:23:00 字数 254 浏览 0 评论 0原文

PHP 5.2.15

我试图用日期函数替换 {date[F]} 。

我有一个效果很好的模式。

preg_replace('/({date\[(.*?)\]})/', date(${2}), $subject);

我已经尝试过 preg_replace_callback 但即使当我使用 create_function() 时它似乎也不起作用,

我尝试的方法大多出现未定义的错误。

PHP 5.2.15

I am trying to replace {date[F]} with the date function.

I have the pattern which works great.

preg_replace('/({date\[(.*?)\]})/', date(${2}), $subject);

I have tried preg_replace_callback but it doesn't seem to work even when I use create_function()

I get mostly undefined errors on my methods tried.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

紫罗兰の梦幻 2025-01-02 07:23:00

您确实需要 /e 修饰符。并且替换表达式必须是字符串:

= preg_replace('/(\{date\[(.*?)\]})/e', 'date("$2")', $subject);

请注意,您还忘记转义第一个 { 大括号。

请参阅 preg_replace#105490 的手动示例。或者 preg_replace_callback 回调必须如何解压匹配参数(这听起来像是您原来的问题)。

You do need the /e modifier. And the replacement expression must be a string:

= preg_replace('/(\{date\[(.*?)\]})/e', 'date("$2")', $subject);

Note that you also forgot to escape the first { curly brace.

See the manual examples for preg_replace#105490. Or how preg_replace_callback callbacks must unpack the match parameter (which sounds like your original issue).

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