N
嘿,我试图剥离标签并仅提取部分直到&#39;帖子&#39;。我试过<p>Blockbuster event so big that the excerpt will not even fit into the allotted space.</p>
<p> The post
<a rel="nofollow" href="http://example.com/event/sample-event/">Sample Event</a>
appeared first on
<a rel="nofollow" href="http://example.com">abc</a>.
</p>
$desc = preg_replace('/<p>(.*)<\/p>/i', '$1', $event->description);
$desc = substr($desc, 0, strpos($desc, 'The post'));
,但这会返回描述的两个部分。为了达到最终结果,我不得不采用一个子字符串。是否有正则表达式来处理这个问题所以我不需要使用substr()?
答案 0 :(得分:1)
制作表达式&#34; ungreedy&#34;:
$desc = preg_replace('/<p>(.*)<\/p>/Ui', '$1', $event->description);
或
$desc = preg_replace('/<p>(.*?)<\/p>/i', '$1', $event->description);
如果您只想保留第一个<p>
标记的内容,preg_match
可能会更简单并保留结果:
preg_match('/<p>(.*)<\/p>/Ui', $event->description, $results);
$desc = $results[1];
这是一个演示:https://ideone.com/KFLdnI