[英]PHP regEx help needed with /*<##> </##>*/
我在这里指出了两个问题,您没有捕获组来替换替换调用中的定界标记,并且Negative Lookahead语法缺少重复运算符 。
$text = preg_replace('%(/\*<##>)(?:(?!\*/).)*(</##>*\*/)%s', '$1new$2', $text);
虽然,您可以用.*?
代替前瞻.*?
因为您正在使用s
(dotall)修饰符。
$text = preg_replace('%(/\*<##>).*?(</##>*\*/)%s', '$1new$2', $text);
或者考虑结合使用环顾四周方法来执行此操作而不捕获组。
$text = preg_replace('%/\*<##>\K.*?(?=</##>\*/)%s', 'new', $text);
经过测试:
$input = "something /*<##>old or something else</##>*/ something other";
echo preg_replace('%(/\*<##>)(.*)(</##>\*/)%', '$1new$3', $input);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.