Change language

How to extract img src and alt from html using PHP?

|
Extract image attributes such as ’ src & # 39 ;, ’ alt ’, ’ height & # 39 ;, ’ width ’ etc., from an HTML page using PHP. This task can be accomplished with the following steps.
  • Load HTML content into a variable (DOM variable).
  • Selects each image in this document.
  • Select an attribute and store its content in a variable.
  • Output as HTML img object or as simple values ​​as required.
Example 1:this example renders the image object as output.
// error_reporting (0); function crawl_page ( $url ) { $dom = new DOMDocument ( ’1.0’ ); // Load HTML content into $dom @ $dom -> loadHTMLFile ( $url ); // Select the entire image, i.e. img tag object $anchors = $dom -> getElementsByTagName ( ’img’ ); // Extract an attribute from each object foreach ( $anchors as $element ) { // Retrieve the value of the src attribute // current image object $src = $element -> getAttribute ( ’src’ ); // Retrieve the value of the alt attribute // current image object $alt = $element -> getAttribute ( ’alt’ ); // Retrieve the value of the height attribute // the current image object $height = $element -> getAttribute ( ’height’ ); // Extract the value of the width attribute from // current image object $width = $element -> getAttribute ( ’width’ ); // Pass the output as an image with the extracted attribute, // you can also print the value of these attributes echo ’</code> <code>.</code> <code> $alt</code> <code>.</code> <code> ’ ; } } crawl_page ( " https://www. google.com/search?q=engineerforengineer&tbm=isch " );  
?>
Output: Example 2:This example displays an image object attribute.
// error_reporting (0); function crawl_page ( $url ) {   $dom = new DOMDocument ( ’ 1.0’ ); // Load HTML content into $dom @ $dom -> loadHTMLFile ( $url ); // Select the entire image, i.e. img tag object $anchors = $dom -> getElementsByTagName ( ’img’ ); // Extract an attribute from each object foreach ( $anchors as $element ) { // Retrieve the value of the src attribute // current image object $src = $element -> getAttribute ( ’src’ ); // Retrieve the value of the alt attribute // current image object $alt = $element -> getAttribute ( ’alt’ ); // Retrieve the value of the height attribute // the current image object $height = $element -> getAttribute ( ’height’ ); // Extract the value of the width attribute from // current image object $width = $element -> getAttribute ( ’width’ ); // Display the output as the value of these attributes echo ’src =’ . $src .
 alt = ’
. $alt .
 height = ’
. $height .
 width = ’
. $width .
; } } crawl_page ( " https://www. google.com/search?q=flowers&tbm=isch " );  
?>
Output:

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method