php1 Min Read

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

Gorav Singal

August 20, 2017

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
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

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…

Security Ticketing and Incident Response

Security Ticketing and Incident Response

The worst time to figure out your incident response process is during an…

Security Mindset for Engineers — Think Like an Attacker

Security Mindset for Engineers — Think Like an Attacker

Most engineers think about security the way they think about flossing — they…

Secrets Management — Vault, SSM, and Secrets Manager

Secrets Management — Vault, SSM, and Secrets Manager

I’ve watched a production database get wiped because someone committed a root…

Penetration Testing Basics for Developers

Penetration Testing Basics for Developers

Most developers think of penetration testing as something a separate security…

OWASP Top 10 for Cloud Applications

OWASP Top 10 for Cloud Applications

The OWASP Top 10 was written for traditional web applications. But in the cloud…