1
$content = preg_replace("~(<a href=\"(.*)\">\w+)~iU", '', $content);
$ok = preg_replace("~(</a>)~iU", '', $content);
echo $ok;

I need to control the $content...

I want to remove all link in the $content....

even <a href="xx"><img xxxx> </a> all to remove A tag Just save <img xxx>...

How can I do?

I need edit the REGEX??

why I just can del the first one

Sky
  • 157
  • 10

1 Answers1

3

You can replace the anchors with their contents using 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();
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309