[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Find image path from string using preg_match ?

Posted on 2009-05-14
5
Medium Priority
?
639 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
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 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