删除所有href';包装纸';除非href包含特定值:PHP
内容:删除所有href';包装纸';除非href包含特定值:PHP,php,filter,preg-replace,Php,Filter,Preg Replace,内容: <a href="http://www.lipsum.com/">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry. <a href="http://www.google.com/1111/2222/3333">Lorem Ipsum</a> has been the industrys standard dummy text e
<a href="http://www.lipsum.com/">Lorem Ipsum</a> is simply dummy text
of the printing and typesetting industry.
<a href="http://www.google.com/1111/2222/3333">Lorem Ipsum</a> has been the industrys
standard dummy text ever since the 1500s, when an unknown printer
took a <a href="http://gallery.com">galley</a> of type and scrambled
it to make a type specimen <a href="http://www.google.com/1111/3333/4444">book</a>.
我想要这个结果:选定的href值是href=”http://google.com/1111/3333****
仅限
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industrys standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
specimen <a href="http://www.google.com/1111/3333/4444">book</a>.
Lorem Ipsum只是印刷和排版行业的虚拟文本。
自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,
当一个不知名的印刷商拿着一个铅字厨房,把它拼凑成一个铅字
样品
有人知道怎么做吗?希望你能理解这个问题。提前谢谢。用正则表达式解析/转换HTML内容不是个好主意。但对于您的小片段,并考虑到您需要在删除自身时保留链接文本(例如,
“Lorem Ipsum”
),您可以使用以下preg\u replace
解决方案:
$html = '<a href="http://www.lipsum.com/">Lorem Ipsum</a> is simply dummy text
of the printing and typesetting industry.
<a href="http://www.google.com/1111/2222/3333">Lorem Ipsum</a> has been the industrys
standard dummy text ever since the 1500s, when an unknown printer
took a <a href="http://gallery.com">galley</a> of type and scrambled
it to make a type specimen <a href="http://www.google.com/1111/3333/4444">book</a>.';
$re = '/<a href="http:\/\/(?!www\.google\.com\/1111\/3+\/[^>]+).*?>([^<>]+)<\/a>/m';
$result = preg_replace($re, "$1", $html);
echo $result;
输出:
Lorem Ipsum is simply dummy text
of the printing and typesetting industry.
Lorem Ipsum has been the industrys
standard dummy text ever since the 1500s, when an unknown printer
took a galley of type and scrambled
it to make a type specimen <a href="http://www.google.com/1111/3333/4444">book</a>.
<html><body>Lorem Ipsum is simply dummy text
of the printing and typesetting industry.
Lorem Ipsum has been the industrys
standard dummy text ever since the 1500s, when an unknown printer
took a galley of type and scrambled
it to make a type specimen <a href="http://www.google.com/1111/3333/4444">book</a>.</body></html>
Lorem Ipsum只是一个虚拟文本
印刷和排版行业的一员。
Lorem Ipsum一直是该行业的领导者
16世纪以来的标准虚拟文本,当未知打印机
拿了一个类型的厨房,然后爬了起来
它需要制作一个类型样本。
$dom = new \DOMDocument();
$dom->loadHTML($html);
$xpath = new \DOMXPath($dom);
$nodes = $xpath->query("//a[not(contains(@href, 'http://www.google.com/1111/3333'))]");
foreach ($nodes as $n) {
$n->parentNode->replaceChild($dom->createTextNode($n->nodeValue), $n);
}
echo $dom->saveHTML($dom->documentElement);
<html><body>Lorem Ipsum is simply dummy text
of the printing and typesetting industry.
Lorem Ipsum has been the industrys
standard dummy text ever since the 1500s, when an unknown printer
took a galley of type and scrambled
it to make a type specimen <a href="http://www.google.com/1111/3333/4444">book</a>.</body></html>