当前位置:Gxlcms > PHP教程 > php中Warning:preg_match_all():Compilationfailed:lookbehindassertion

php中Warning:preg_match_all():Compilationfailed:lookbehindassertion

时间:2021-07-01 10:21:17 帮助过:14人阅读

今天在测试正则表达式中相关环视的代码时,发现有个错误
错误信息如下:
Warning: preg_match_all(): Compilation failed: lookbehind assertion is not fixed length at offset
代码如下:
  1. $str='infostrong';
  2. preg_match_all('/(?<=<(\w+)[^>]*>).*(?=<\/\1>)/',$str,$match);
  3. var_dump($match);
执行上面的代码就会报错,在网上查看下,说php中逆序环视(包括逆序肯定环视 ?<=exp、逆序否定环视 ?
将上面的代码改为:
  1. $str='infostrong';
  2. preg_match_all('/(?<=<(\w)>).*(?=<\/\1>)/',$str,$match);
  3. var_dump($match);
但这种只能匹配b标签中strong内容
其实,不用环视来匹配,直接用分组来捕获内容,只是这样要在返回的结果中筛选一下
  1. $str='infostrong';
  2. preg_match_all('/(<(\w+)[^>]*>)(.*)(<\/\2>)/',$str,$match);
  3. print_r($match[3]);//Array ( [0] => info [1] => strong )


人气教程排行