// 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
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
Have a look at DOM and XPath instead of regular expressions.
http://www.php.net/manual/en/book.dom.php
Micha
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