Extract Total Number of Image And Missing Alt Attribute using php

Here i will show you how can you extract total number of images from HTML content and find out missing alt attribute of the image using very simple code using php.

<?php
$html = '
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>Extract Total Number of Image And Missing Alt Attributte</title>
</head>
<body>
	<img src="test.png">
	<img src="test.jpg" alt="Testing">
	<img src="test.gif">
</body>
</html>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$images = $dom->getElementsByTagName("img");
$allImagesCount = 0;
$altEmptyTag = 0;
foreach ($images as $image)
{
	if (!$image->hasAttribute("alt"))
	{
		$altEmptyTag++;
	}
	$allImagesCount++;
}
//echo $dom->saveHTML();
echo 'Total number of mages: '.$allImagesCount.'<br>';
echo 'Total number of mages missing alt attributes: '.$altEmptyTag.'<br>';
?>
Tags: How To Retrieve An Image Attachment’s Alt Text, How to get image title/alt attribute, How to extract img src, title and alt from html using php, HTML DOM images Collection, php get all images from url, php extract images from html, php html parser, simple html dom get attribute value, php script to download all images from url, simple_html_dom.php download, php image scraper , php preg_match get img src, wordpress get image alt text, the images on this page contain alt attributes, get image title attribute using php

Leave a Reply

Related Posts: You may like

How to extract Heading tags using PHP from string

How to remove whitespace from string using php

Remove All Special Characters From String PHP

Redirecting HTTP to HTTPS with PHP

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.