php – preg_replace在省略时插入默认值
发布时间:2020-12-13 17:45:06 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试为preg_replace工作一些正则表达式,当它不在主题中时会插入默认键=“值”. 这就是我所拥有的: $pattern = '/[section([^]]+)(?!type)]/i';$replacement = '[section$1 type="wrapper"]'; 我希望这转变: [section title="This is the title"]
我正在尝试为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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |