例如
<?php
$html = "<p>Lorem ipsum lorem ipsum lorem ipsum</p>
<p>Lorem ipsum lorem ipsum lorem ipsum</p>
<p>Read : SOME ARTICLE</p>
<p>Lorem ipsum lorem ipsum lorem ipsum";
?>
我想删除 <p>Read : SOME ARTICLE</p>
,也许使用php regex。
如何开始<p>Read :
**** </p>
已经不见了。
由于
答案 0 :(得分:2)
您应该使用DOMDocument
代替Regex
。
您可以选择使用LIBXML_HTML_NODEFDTD
和LIBXML_HTML_NOIMPLIED
来避免html
代码。
<?php
ini_set('display_errors', 1);
$html = "<p>Lorem ipsum lorem ipsum lorem ipsum</p>
<p>Lorem ipsum lorem ipsum lorem ipsum</p>
<p>Read : SOME ARTICLE</p>
<p>Lorem ipsum lorem ipsum lorem ipsum";
$domDocument = new DOMDocument();
$domDocument->loadHTML($html,LIBXML_HTML_NODEFDTD|LIBXML_HTML_NOIMPLIED);
$domXPath = new DOMXPath($domDocument);
$results = $domXPath->query("//p[contains(.,'Read :')]");//querying p node which contains `Read: `
$nodeToremove=$results->item(0);
$nodeToremove->parentNode->removeChild($nodeToremove);//Removing node
echo $domDocument->saveHTML();
答案 1 :(得分:1)
尝试使用preg_relace
$html = "<p>Lorem ipsum lorem ipsum lorem ipsum</p>
<p>Lorem ipsum lorem ipsum lorem ipsum</p>
<p>Read : SOME ARTICLE</p>
<p>Lorem ipsum lorem ipsum lorem ipsum";
$html = preg_replace("/(<p>Read :.*?<\/p>)/", "", $html);
echo $html;