Solved

Find image path from string using preg_match ?

Posted on 2009-05-14
5
629 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
Comment Utility
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
Comment Utility
its not working..
0
 
LVL 18

Accepted Solution

by:
Hube02 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now