欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php中Warning: preg_match_all(): Compilation failed: lookbehind assertion

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


上一篇:

下一篇: