Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find image path from string using preg_match ?

Posted on 2009-05-14
5
Medium Priority
?
638 Views
Last Modified: 2012-05-07
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="">'


 

0
Comment
Question by:amitbravo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Hube02
ID: 24383610
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

0
 

Author Comment

by:amitbravo
ID: 24567282
its not working..
0
 
LVL 18

Accepted Solution

by:
Hube02 earned 2000 total points
ID: 24567379
When I run the code that I provided I get the following output:

Array
(
    [0] => <img src="http://www.techie-diva.com/com_img.php?ID=2&img=vatican-museums-hall.jpg" alt="">
    [1] => <img src="http://www.techie-diva.com/com_img.php?ID=5&img=secondimage.jpg" alt="">
)

to output one of the first value you would do something like:

echo matches[0][0].

If this is not what you are looking for can you please explain further, maybe I'm missing what it is you are trying to do.
0
 

Author Closing Comment

by:amitbravo
ID: 31590415
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..
0
 
LVL 18

Expert Comment

by:Hube02
ID: 24581085
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.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question