2017-02-27 3 views
0

DOMDocument로로드하고 특정 태그를 바꿀 텍스트가 있습니다.DOMDocument를 사용하여 HTML을 구문 분석하고 특정 태그를 바꾸는 방법

<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

답변

0

$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(); 
있는 DOMDocument

를 사용하여 특정 HREF를 교체하려면