加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP正则表达式不起作用 – 解析{{tags}}

发布时间:2020-12-13 21:54:01 所属栏目:PHP教程 来源:网络整理
导读:我想要做的是解析以下字符串,使{{}}内的所有占位符都被“default”指示的值替换.例如,{{time,default = noon}}应该成为正午. 这是我试过的代码: $input = 'It was a {{color,default=black}} and scary {{phase}}. As the {{animals,default=dogs}} {{make_
我想要做的是解析以下字符串,使{{}}内的所有占位符都被“default”指示的值替换.例如,{{time,default = noon}}应该成为正午.

这是我试过的代码:

$input = 'It was a {{color,default=black}} and scary {{phase}}. As the {{animals,default=dogs}} {{make_sound,default=barked}} and the trees swayed {{setting,default=to the breeze}},a {{size,default=}} {{monster,default=troll}} that emerged from the shadows.';
$input = preg_replace('/{{.*default=(w+)}}/i','$1',$input);
echo $input;

产生的输出是:输出:它是从阴影中出现的巨魔.

为什么剩余的模式无法正确解析?

解决方法

{{[^}]*bdefault=([^}]*)}}

您可以尝试这个.替换$1.查看演示.

https://regex101.com/r/aG0sF5/4

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读