I have a text which i want to load as DOMDocument and replace specific tags.
<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>
I want to remove tags and output should be:
**link1**
There's only one thing people of the Internet love more than an absolutely epic
**link2**
mistake on live television
**link3**
Code:
$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();
it's giving me output like:
<a>link1</a> etc...
I how do i get rid of tags and output only text Ex. link1
$text = strip_tags($link);
Refer to this: http://php.net/manual/en/function.strip-tags.php
To replace specific href using DOMDocument
$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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.