Created
May 30, 2018 09:43
-
-
Save ydegtyar/960264624e4138059b338a1af33c68bd to your computer and use it in GitHub Desktop.
Create DomElement from html string in PHP. You need to use DomDocument and than extract your element.
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
/** | |
@param string $html | |
@return \DomElement | |
*/ | |
function getDomElementFromHtml($html) | |
{ | |
$document = new \DOMDocument(); | |
$document->loadHTML($html); | |
return $document->getElementsByTagName('*')->item(0); | |
} | |
$html = '<a href="https://example.com">Example.com</a>'; | |
$linkDomElement = getDomElementFromHtml($html); | |
echo $linkDomElement->textContent; // Example.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment