在PHP中没有源链接的情况下从字符串中删除锚标记

时间:2019-03-09 04:58:29

标签: php string preg-replace

您好,下面是我的字符串

$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>'";

之间的字符串包含n个链接(anchor标签),我需要从字符串中删除除源链接之外的所有链接(anchor标签)。 下面是尝试过的

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

它找到每个链接(锚标记)并替换它,但是最后我需要替换除源之外的所有链接。感谢任何帮助

2 个答案:

答案 0 :(得分:1)

使用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();

答案 1 :(得分:-1)

尝试一下:

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