php – preg_replace在省略时插入默认值

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – preg_replace在省略时插入默认值脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为PReg_replace工作一些正则表达式,当它不在主题中时会插入认键=“值”.

这就是我所拥有的:

$pattern = '/\[section([^\]]+)(?!tyPE)\]/i';
$replacement = '[section$1 type="wrapper"]';

我希望这转变:

[section tITle="This is the title"]

成:

[section title="This is the title" type="wrapper"]

但是当有价值时,我不希望它匹配.这意味着:

[section title="This is the title" type="full"]

会保持不变.

我正在使用负面预测错误.第一部分将始终匹配,(?!类型)变得无关紧要.我不知道如何放置它以便它可以工作.有任何想法吗?

解决方法

应该

/\[(?![^\]]*type)section([^\]]*)\]/i
   -------------         ------
         |                  |->your required data in group 1
         |->;match further only if there is no type!

试试吧here

脚本宝典总结

以上是脚本宝典为你收集整理的php – preg_replace在省略时插入默认值全部内容,希望文章能够帮你解决php – preg_replace在省略时插入默认值所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。