PHP正则表达式删除特定单词的标记

时间:2017-09-18 11:11:57

标签: php regex

例如

<?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>已经不见了。

由于

2 个答案:

答案 0 :(得分:2)

您应该使用DOMDocument代替Regex

您可以选择使用LIBXML_HTML_NODEFDTDLIBXML_HTML_NOIMPLIED来避免html代码。

Try this code snippet here

<?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;

DEMO