Link to home
Start Free TrialLog in
Avatar of jej07
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);
		 
	}			
}

Open in new window

Avatar of SamsonChung
SamsonChung
Flag of Canada image

I wouldn't call preg yet, think of it in a simpler manner...

$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.

Avatar of Beverley Portlock
Change the second regex to be like this

     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

Open in new window


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

Avatar of jej07
jej07

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.
$row = '&lt;p&gt;Praesent non lorem nisi. &lt;img class="imgWhiteBorder" src="http://www.google.com/images/logos/ps_logo2.png" alt="Logo 2" title="Google\'s Logo" width="100" align="left" /&gt;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.&lt;img class="imgWhiteBorder" src="http://www.google.com/images/logos/ps_logo2.png" alt="Logo 2" title="Google\'s Logo" width="100" align="left" /&gt;&lt;p&gt;';

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
		 
	}

Open in new window

The < character isn't matching because the < characters in your text have been html-encoded to &lt; - do you know why that might be?
Avatar of jej07

ASKER

@TerryAtOpus, Good catch. It must have been inserted using the htmlspecialchars function.
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
		 
	}

Open in new window

SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jej07

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jej07

ASKER

Fantastic!! Thank you so much for the help and for introducing me to array_search.