1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$html = '<img id="12" border="0" src="/images/image.jpg" alt="Image" width="100" height="100" />'; $doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $src = $xpath->evaluate("string(//img/@src)"); # "/images/image.jpg" Or for those who really need to save space: $xpath = new DOMXPath(@DOMDocument::loadHTML($html)); $src = $xpath->evaluate("string(//img/@src)"); And for the one-liners out there: $src = (string) reset(simplexml_import_dom(DOMDocument::loadHTML($html))->xpath("//img/@src")); |
Leave a reply