<code><br>$content = '
<p>
这里是帖子的内容。
</p>
<p>
@@##@@
</p>
<p>
<span>这里是帖子的其他内容</span>
</p>
<pre class="brush:cf;toolbar:false">$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);匹配的结果:
<code>Array</code><code>(
[0] => Array
(
[0] =>
<p>
这里是帖子的内容。
</p>
)
[1] => Array
(
[0] =>
这里是帖子的内容。
)
)
</code>为什么p标签之间有img span等其他标签的内容不能被匹配呢?
<code><br>$content = '
<p>
这里是帖子的内容。
</p>
<p>
@@##@@
</p>
<p>
<span>这里是帖子的其他内容</span>
</p>
<pre class="brush:cf;toolbar:false">$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);匹配的结果:
<code>Array</code><code>(
[0] => Array
(
[0] =>
<p>
这里是帖子的内容。
</p>
)
[1] => Array
(
[0] =>
这里是帖子的内容。
)
)
</code>为什么p标签之间有img span等其他标签的内容不能被匹配呢?
已更新,preg_match_all('#<p>]*)>([\s\S]*?)</p>
<p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>#',$content,$matches);
这是完整的写法,你的问题在于你错误的以为[^(?:?p>)]代表的是除<p>>或者<code>
[^(?:?p>)]等同于[^?:?p>]这样看就明白了吧
不用正则,xpath搞定

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号