$content = preg_replace("~(<a href=\"(.*)\">\w+)~iU", '', $content);
$ok = preg_replace("~(</a>)~iU", '', $content);
echo $ok;
我需要控制$ content ...
我想删除$ content ....
中的所有链接甚至<a href="xx"><img xxxx> </a>
全部删除A标记只需保存<img xxx>
...
我该怎么办?
我需要编辑REGEX ??
为什么我只能将第一个
答案 0 :(得分:3)
您可以使用DOMDocument
替换锚点及其内容:
$html = <<<'EOS'
<a href="xx"><img src="http://example.com"> </a>
<a href="xx"><img src="http://example.com"> </a>
EOS;
$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//a') as $anchor) {
$fragment = $doc->createDocumentFragment();
// collecting the child nodes
foreach ($anchor->childNodes as $node) {
$fragment->appendChild($node);
}
// replace anchor with all child nodes
$anchor->parentNode->replaceChild($fragment, $anchor);
}
echo $doc->saveHTML();