Created
September 8, 2021 10:46
-
-
Save borsna/0439289f32a0b1064df3fccde323b81f to your computer and use it in GitHub Desktop.
Html links to plain text in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function htmlLinksToPlainText(string $html): string{ | |
$htmlDom = new \DOMDocument; | |
@$htmlDom->loadHTML('<?xml encoding="utf-8" ?>'.$html); | |
$links = $htmlDom->getElementsByTagName('a'); | |
foreach($links as $link){ | |
$linkHtml = $htmlDom->saveHTML($link); | |
$linkText = $link->nodeValue; | |
$linkHref = $link->getAttribute('href'); | |
$html = str_replace($linkHtml, $linkText.' ('.$linkHref.') ', $html); | |
} | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment