Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get src and alt from an img tag?

94 views
Skip to first unread message

damezumari

unread,
Jul 24, 2008, 9:03:48 AM7/24/08
to
At http://www.weberdev.com/get_example-4625.html I found these two
functions:

// get image alt descriptions
function get_image_alt($file){
$h1count = preg_match_all('/(alt=.)([a-zA-Z0-9\s]{1,})/',$file,
$patterns);
$res = array();
array_push($res,$patterns[2]);
array_push($res,count($patterns[2]));
return $res;
}

// retrieve images on the site
function get_images($file){
$h1count = preg_match_all('/(<img)\s (src="([a-zA-Z0-9\.;:\/\?&=_|
\r|\n]{1,})")/isxmU',$file,$patterns);
$res = array();
array_push($res,$patterns[3]);
array_push($res,count($patterns[3]));
return $res;
}

However, I want to list for each image its src and its alt. If an
image has no alt its value could be 'No value'.

Kind regards,

Jan Nordgreen

Erwin Moller

unread,
Jul 24, 2008, 12:09:21 PM7/24/08
to
damezumari schreef:

Hi Jan,

I don't see how you can do that easily in one regex.
You'll find both the following in the wild:
<img src=".." alt="..">
and
<img alt=".." src="..">

So you'll have to alternate inside the regex for both and later fix the
results of your matches.
Sounds unneeded difficult to me, but I am no regex wizard. :-)

I think in your situation I would simply grab all between all '<img' and
'>' (with a regex), and then inspect the content of each match (string)
for 'alt=' and 'src=' by looping over them.
Should be straightforward.

just my 2 cent.

Regards,
Erwin Moller

Michael Fesser

unread,
Jul 24, 2008, 1:39:42 PM7/24/08
to
.oO(damezumari)

Have a look at DOM and XPath instead of regular expressions.

http://www.php.net/manual/en/book.dom.php

Micha

damezumari

unread,
Jul 26, 2008, 2:32:53 AM7/26/08
to
Hi Erwin,

Thanks for the advice.

http://easyquestion.net/testing/imagetags2.php lists the images of
three web pages using your idea.

I would assume that my coding is far from optimal, but at least it
seems to work. Any suggestions for improving the code is more than
welcome.

I have started to study SEO and I want the user to be able to add or
edit the images' alt values in a simple way. As you can see there is
no Submit button so the program is far from finished.

If this service already exists somewhere on the Internet I have not
been able to find it.

Kind regards,

Jan Nordgreen

0 new messages