Solved

Find image path from string using preg_match ?

Posted on 2009-05-14
5
634 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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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