Link to home
Start Free TrialLog in
Avatar of amitbravo
amitbravo

asked on

Find image path from string using preg_match ?

I have some text like

$text= 'blah blah .. <img src="http://www.techie-diva.com/com_img.php?ID=2&img=vatican-museums-hall.jpg" alt=""> ..... blah blah .. <img src="http://www.techie-diva.com/com_img.php?ID=5&img=secondimage.jpg" alt="">. .. . .blah blah ... ';

I need to find if image path like <img src="http://www.techie-diva.com/com_img.php....... alt=""> exists.

 if exists then I need to retrieve one image 's complete html code back like > 
<img src="http://www.techie-diva.com/com_img.php?ID=2&img=vatican-museums-hall.jpg" alt="">

could it be done with preg_match ? how to create pattern of search ?
like ...
Find > from '<img src="http://www.techie-diva.com/com_img.php...'  to 'alt="">'


 

Avatar of Hube02
Hube02
Flag of United States of America image

Try this,

You'll need to view the source to actually see what is in the array due to the values being image tags.
<?php
  
  $text = 'blah blah .. <img src="http://www.techie-diva.com/com_img.php?ID=2&img=vatican-museums-hall.jpg" alt=""> ..... blah blah .. <img src="http://www.techie-diva.com/com_img.php?ID=5&img=secondimage.jpg" alt="">. .. . .blah blah ... ';
  
  $regex = '/<img.*?com_img\.php[^>]*>/i';
  
  preg_match_all($regex, $text, $matches);
  
  print_r($matches[0]);
  
?>

Open in new window

Avatar of amitbravo
amitbravo

ASKER

its not working..
ASKER CERTIFIED SOLUTION
Avatar of Hube02
Hube02
Flag of United States of America 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
I apologies . I did not open view source to see how print_r works . moreover I do not rules how to create expression .. please let me know how could i study in details how to create expression using * ? kind of stuff..
To answer the follow up, I learned Regular Expressions by reading http://oreilly.com/catalog/9780596528126/?CMP=AFC-ak_book&ATT=Mastering+Regular+Expressions%2c+Third+Edition%2c and then practicing a lot. Still learning myself actually.