Home > Backend Development > PHP Tutorial > How to Get the Source URL of the First Image in an HTML Document?

How to Get the Source URL of the First Image in an HTML Document?

Linda Hamilton
Release: 2024-12-06 15:40:13
Original
192 people have browsed it

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)");
Copy after login

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)");
Copy after login

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"));
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template