Thiyagarajan
Thiyagarajan

Reputation: 277

Remove anchor tag from string without source link in PHP

Hi below is my string

$str = "It’s been a rough couple of days for <a href='aaa.com'>Apple</a>, as the company is still trying to fix a massive glitch in Facetime that allows consumers to hear audio from users added to group calls before they pick up. It’s not known how long the glitch has existed, but the details of it became widespread knowledge yesterday and prompted Apple to disable the feature entirely. As it turns out, a 14-year-old Arizona teenager discovered the glitch last week while setting up a group chat of Fortnite players, inadvertently finding himself at ground zero for a massive privacy breach debacle.
The teenager discovered the issue on January 20th while setting up a group chat for his friends ahead of a weekend Fortnite session. As he added his pals into the group video chat, he realized that he could hear audio from users he had just added to the group chat before they’d even picked up. He notified his mom, Michele Thompson, who then tried to inform Apple about the glitch.
The <a href='bbb.com'>proactive</a> mother went through several mediums in an effort to garner a response from Apple, including phone, fax, Facebook, and Twitter, where she even tried to reach out to Apple CEO Tim Cook. Unfortunately, none of these mediums garnered an immediate response from Apple.
Source: <a href='sou.com>Source</a>'";

The string in between contains n number of links(anchor tag), I need to remove all the links(anchor tag) from string except source link in end. below am tried one

$item = preg_replace('/<\/?a[^>]*>/','',$str);

It find each links(anchor tag) and replace it, but I need replace all links except source in the end.Any help appreciated

Upvotes: 0

Views: 206

Answers (1)

Toto
Toto

Reputation: 91375

Use DOMDocument:

$html = <<<EOD
It’s been a rough couple of days for <a href='aaa.com'>Apple</a>, as the company is still trying to fix a massive glitch in Facetime that allows consumers to hear audio from users added to group calls before they pick up. It’s not known how long the glitch has existed, but the details of it became widespread knowledge yesterday and prompted Apple to disable the feature entirely. As it turns out, a 14-year-old Arizona teenager discovered the glitch last week while setting up a group chat of Fortnite players, inadvertently finding himself at ground zero for a massive privacy breach debacle.
The teenager discovered the issue on January 20th while setting up a group chat for his friends ahead of a weekend Fortnite session. As he added his pals into the group video chat, he realized that he could hear audio from users he had just added to the group chat before they’d even picked up. He notified his mom, Michele Thompson, who then tried to inform Apple about the glitch.
The <a href='bbb.com'>proactive</a> mother went through several mediums in an effort to garner a response from Apple, including phone, fax, Facebook, and Twitter, where she even tried to reach out to Apple CEO Tim Cook. Unfortunately, none of these mediums garnered an immediate response from Apple.
Source: <a href='sou.com'>Source</a>
EOD;

$dom = new DOMDocument;
$dom->loadHTML($html);
$aTags = $dom->getElementsByTagName('a');
foreach ($aTags as $aTag) {
    if ($aTag->nodeValue == 'Source') continue;
    $aTag->parentNode->removeChild($aTag);
}
echo $dom->saveHTML();

Upvotes: 1

Related Questions