$content = '
这里是帖子的内容。
这里是帖子的其他内容
$user = $this->session->get('auth');
$baa_id=(int)$_POST['baa_id'];
$time = date('Y-m-d H:i:s');
这里是帖子的其他内容
';
preg_match_all('#]*>([^(?:?p>)]*)
#',$content,$matches);
print_r($matches);
匹配的结果:
Array( [0] => Array ( [0] =>这里是帖子的内容。
) [1] => Array ( [0] => 这里是帖子的内容。 ) )
为什么p标签之间有img span等其他标签的内容不能被匹配呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
已更新,
preg_match_all('#<p(\s*|\s[^>]*)>([\s\S]*?)</p>#',$content,$matches);这是完整的写法,你的问题在于你错误的以为
[^(?:</?p>)]代表的是除<p>或者</p>标签以外的匹配。实际上[^(?:</?p>)]等同于[^?:</?p>]这样看就明白了吧不用正则,
xpath搞定