~/php/26-how-get-all-image-tags-html-php-code.md

How to get all image tags from an html - php code

1 min readAugust 20, 2017by Gorav Singal#php-code#fetch-images#php
// TL;DR

Use PHP's DOMDocument class to parse HTML and extract all img tags with their src attribute values for further processing.

How to get all image tags from an html - php code

Problem Statement

I wanted to fetch all image tags from a big html, and wanted to perform some check on the ’src’ value of img tag. This blog is about how I did that.

Solution

See the code below:

$doc = new DOMDocument();
$doc->loadHTML($body);
$tags = $doc->getElementsByTagName('img');
$imgArr = array();

//iterate over all image tags
foreach ($tags as $tag) {
  //get src attribute of an img tag
  $imgSrc = $tag->getAttribute('src');

  //Do your processing with any attribute of img tag
}

Sample Output

https://st.hzcdn.com/simgs/08611ef0050cb085_8-4061/contemporary-bathroom.jpg
https://st.hzcdn.com/simgs/f5c19084044862df_8-4100/modern-bathroom.jpg
https://st.hzcdn.com/simgs/db21d41901d3cc2d_8-7242/contemporary-bathroom.jpg
https://st.hzcdn.com/simgs/dbf10837069daa21_8-1951/contemporary-bathroom.jpg
https://st.hzcdn.com/simgs/e8e1de0e0231e294_8-8943/contemporary-bathroom.jpg
https://st.hzcdn.com/simgs/8c71b1fe0535cbe3_8-8313/contemporary-bathroom.jpg
https://st.hzcdn.com/simgs/e961a3e003fd7b05_8-4430/contemporary-bathroom.jpg
https://st.hzcdn.com/simgs/1ef1a9660cda6923_8-3728/contemporary-bathroom.jpg

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

List all the Node ids which do not have images from my domain

List all the Node ids which do not have images from my domain

I use drupal-7 in my website. I used to write articles and put images in that…

How to connect Php docker container with Mongo DB docker container

How to connect Php docker container with Mongo DB docker container

Goto your command terminal. Type: This will expose port: 27017 by default. You…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

How to install Mongo DB Driver for Php 7.x

How to install Mongo DB Driver for Php 7.x

The simplest way to install driver for php is using pecl. When I tried to run…

Paypal Payment Issue While Validating Payment - Access Denied

Paypal Payment Issue While Validating Payment - Access Denied

Introduction I was using Paypal payment on one of my website, and suddenly lot…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…