Home >Backend Development >PHP Tutorial >How to Get the Source URL of the First Image in an HTML Document?
How to Extract the Source URL of the First Image in an HTML Document
Extracting the source URL (SRC value) of the first image in an HTML document can be achieved using several approaches, including:
Using DOMDocument and DOMXPath:
This approach utilizes the DOMDocument and DOMXPath classes to navigate and manipulate the HTML structure. By creating a new DOMDocument object, loading the HTML into it, and using DOMXPath to evaluate the '/img/@src' path, you can obtain the SRC value of the first image.
Example:
$html = '<img 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)");
Using SimpleXMLElement::xpath():
For a more concise approach, you can combine DOMDocument manipulation and SimpleXMLElement::xpath():
Example:
$xpath = new DOMXPath(@DOMDocument::loadHTML($html)); $src = $xpath->evaluate("string(//img/@src)");
Using SimpleXMLImportDom() and array_shift():
This approach simplifies the process further, leveraging SimpleXMLImportDom() to convert the DOMDocument into a SimpleXMLElement and using array_shift() to extract the first element:
Example:
$src = (string) reset(simplexml_import_dom(DOMDocument::loadHTML($html))->xpath("//img/@src"));
Regardless of the chosen approach, these techniques provide effective methods to retrieve the SRC attribute value of the first occurring image tag in an HTML document.
The above is the detailed content of How to Get the Source URL of the First Image in an HTML Document?. For more information, please follow other related articles on the PHP Chinese website!