php|August 20, 2017|1 min read

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

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

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

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…