PHP: Extract IMG tags from HTML pages

Hi All,

Does anyone know how i can extract IMG info from 200 html files?

Sample output below;

<img title="My title 1" src="title1.gif" alt="title 1" style="border: 0px solid ; width: 740px; height: 80px;" />
<img title="My title 2" src="title2.gif" alt="title 2" style="border: 0px solid ; width: 740px; height: 80px;" />
<img title="My title 3" src="title3.gif" alt="title 3" style="border: 0px solid ; width: 740px; height: 80px;" />
<img title="My title 4" src="title4.gif" alt="title 4" style="border: 0px solid ; width: 740px; height: 80px;" />


Thanks in adv
LVL 2
detox1978Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PatrickAdrichemCommented:
You could write a pHp dom parser (or find one on google) and then use XPath to extract the required images.

for more information about domparsing with xpath you can check out php.net

http://www.php.net/manual/en/class.domxpath.php
0
detox1978Author Commented:
Thanks for the links.

Leakim, i saw that post but it doesn't really do anything strip_html can't do.

PatrickAdrichem, need a bit more help than that.  i was looking to use the gethtml and strip_html on a loop.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

detox1978Author Commented:
sorry strip_tags
0
PatrickAdrichemCommented:
How about this?

<?php

$oDocument = new DOMDocument();
@$oDocument->loadHTMLFile("http://www.google.com");

$oXpathDocument = new DOMXPath($oDocument);

foreach ($oXpathDocument->query("//img") as $oImage)
{
	var_dump($oImage->getAttribute("src"));
	var_dump($oImage->getAttribute("alt"));
	var_dump($oImage->getAttribute("title"));
	var_dump($oImage->getAttribute("style"));

	// more about $oImage http://nl2.php.net/manual/en/class.domelement.php
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
detox1978Author Commented:
I get the following error.

Fatal error: Class 'DOMDocument' not found
0
detox1978Author Commented:
I have root access and the server is CentOS with yum, in case i need to install something?
0
PatrickAdrichemCommented:
I've googled this for you, did not know it myself eather, but apperently you need the PHP-XML library installed. At our office all PHP-*'s are installed automatically so i didn't know it was a separate library
0
detox1978Author Commented:
I'll look into it this evening.
0
detox1978Author Commented:
Thanks, i've installed php-xml via "yum install php-xml" and the script now works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.