我如何解析HTML并使用DOMDocument替换特定标记

我如何解析HTML并使用DOMDocument替换特定标记

问题描述:

我有一个文本,我想加载为DOMDocument并替换特定的标记。我如何解析HTML并使用DOMDocument替换特定标记

<a href="https://www.google.co.in/dsfethtrw">link1</a> 

There's only one thing people of the Internet love more than an absolutely epic 

<a href="https://www.google.co.in/dsfethtrfersgest">link2</a> 
mistake on live television 

<a href="https://www.google.co.in/ewferagre">link3</a> 

我想删除标签和输出应该是:

**link1** 

     There's only one thing people of the Internet love more than an absolutely epic 

     **link2**  
mistake on live television 

     **link3** 

代码:

$dom = new DOMDocument; 
$dom->loadHTML($entity->body[$field_lang][0]['value']); 
foreach ($dom->getElementsByTagName('a') as $node) { 
    $node->removeAttribute('href'); 
} 
$entity->body[$field_lang][0]['value'] = $dom->saveHTML(); 

它给我的输出,如:

<a>link1</a> etc... 

我怎么做我摆脱标签并仅输出文本Ex。 link1

要使用DOM文档

$xml = new DOMDocument(); 
$xml->loadHTML($entity->body[$field_lang][0]['value']); 

$links = $xml->getElementsByTagName('a'); 

//Loop through each <a> tags and replace them by their text content 
for ($i = $links->length - 1; $i >= 0; $i--) { 
    $linkNode = $links->item($i); 
    $lnkText = $linkNode->textContent; 

    if ($url == $linkNode->attributes->item(0)->nodeValue) { 
    $newTxtNode = $xml->createTextNode($lnkText); 
    $linkNode->parentNode->replaceChild($newTxtNode, $linkNode); 
    } 
} 
$entity->body[$field_lang][0]['value'] = $xml->saveHTML(); 
更换特定的href

$ text = strip_tags($ link);

请参阅本:http://php.net/manual/en/function.strip-tags.php