jej07
asked on
Search string for img src and alt, using PHP
I would like to search a long string for the first instance of img. If found, it would then extract the src and title. The order may or may not be consistent. I thought this could be done using preg_match_all, but it doesn't seem to be working..
while($row = mysql_fetch_array( $result )) {
preg_match_all('/<img[^>]+>/i',$row['article'], $imgResult); // Find all images
if ($imgResult != '') {
preg_match_all('/(title|src)=("[^"]*")/i',$imgResult[0], $imgAttributes);
}
}
Change the second regex to be like this
I'm assuming that the first regex works, but it looks like it should. This second will give you all the tags in $imgAttributes[1] and you can look for SRC and TITLE in there. The corresponding values will have the same indices in $imagAttributes[2]
Run it and see, the output is fairly self-explantory
preg_match_all( '#\s+([a-z]+)\s*?=\s*?[\'|"]([^"|\']*?)[\'|"]#i', $imgResult[0], $imgAttributes );
echo "<pre>"; print_r( $imgAttributes ); echo "</pre>"; // testing - so you can see the output
I'm assuming that the first regex works, but it looks like it should. This second will give you all the tags in $imgAttributes[1] and you can look for SRC and TITLE in there. The corresponding values will have the same indices in $imagAttributes[2]
Run it and see, the output is fairly self-explantory
ASKER
@bportlock,
I simplified my code so you could also see an example string. It looks like I may be having trouble with both regex.
I simplified my code so you could also see an example string. It looks like I may be having trouble with both regex.
$row = '<p>Praesent non lorem nisi. <img class="imgWhiteBorder" src="http://www.google.com/images/logos/ps_logo2.png" alt="Logo 2" title="Google\'s Logo" width="100" align="left" />In hac habitasse platea dictumst. Praesent viverra rutrum feugiat. Nulla velit lorem, sodales et porta a, aliquam et leo. Morbi sed odio at neque faucibus egestas.<img class="imgWhiteBorder" src="http://www.google.com/images/logos/ps_logo2.png" alt="Logo 2" title="Google\'s Logo" width="100" align="left" /><p>';
preg_match_all('/<img[^>]+>/i',$row, $imgResult); // Find all images
echo "<pre>"; print_r( $imgResult ); echo "</pre>";
if ($imgResult != '') {
preg_match_all( '#\s+([a-z]+)\s*?=\s*?[\'|"]([^"|\']*?)[\'|"]#i', $imgResult[0], $imgAttributes );
echo "<pre>"; print_r( $imgAttributes ); echo "</pre>"; // testing - so you can see the output
}
The < character isn't matching because the < characters in your text have been html-encoded to < - do you know why that might be?
ASKER
@TerryAtOpus, Good catch. It must have been inserted using the htmlspecialchars function.
Okay, the first regex works, but not the second.
Okay, the first regex works, but not the second.
preg_match_all('/<img[^>]+>/i',html_entity_decode($row), $imgResult); // Find all images
echo "<pre>"; print_r( $imgResult ); echo "</pre>";
if ($imgResult != '') {
preg_match_all( '#\s+([a-z]+)\s*?=\s*?[\'|"]([^"|\']*?)[\'|"]#i', $imgResult[0], $imgAttributes );
echo "<pre>"; print_r( $imgAttributes ); echo "</pre>"; // testing - so you can see the output
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@TerryAtOpus, Thank you!
I'm looking at the results, and I'm not sure how I can pull out the src and title for only the first image. Especially if the order is different.
For example, this time I have 2 images in $row with tags in order of class, src,alt, title, width and align. But next time I might have 4 images in $row with only the alt and src tags. How do I extract just the src and title from the first image when the tag order could be different?
I'm looking at the results, and I'm not sure how I can pull out the src and title for only the first image. Especially if the order is different.
For example, this time I have 2 images in $row with tags in order of class, src,alt, title, width and align. But next time I might have 4 images in $row with only the alt and src tags. How do I extract just the src and title from the first image when the tag order could be different?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fantastic!! Thank you so much for the help and for introducing me to array_search.
$position_of_img = strpos($row,'img') this gives you the position of the first occurance of the text img
now, take this and alter your algorithm so it reads 'all img position' and than, extract the src using Substring.