Solved

Extract IMG from code

Posted on 2011-02-17
10
836 Views
Last Modified: 2012-05-11
Hi;
I have an array which in  $rss_channel["ITEMS"][$i]["DESCRIPTION"]   are my links to my images but images comes in this format

<p><a href="http://www.somesite.com/latourell_falls_columbia_river_oregon___1600x1-wallpapers.html" ><img src="http://www.somesite.com/thumbs/latourell_falls_columbia_river_oregon___1600x1-t2.jpg" align="left" /></a></p><br clear=all>

Whst would be the righ tsyntax with preg_match_all or anyother method I can get the link out in this format:

img src="http://www.somesite.com/thumbs/latourell_falls_columbia_river_oregon___1600x1-t2.jpg

Thanks a lot for the help
0
Comment
Question by:babak62
  • 6
  • 4
10 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34921743
Generated from myregextester.com:

PHP Code Example:
<?php
$sourcestring="your source string";
preg_match_all('/img[^>]*src="[^"]*"/',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
 
$matches Array:
(
    [0] => Array
        (
            [0] => img src="http://www.somesite.com/thumbs/latourell_falls_columbia_river_oregon___1600x1-t2.jpg"
        )

)
0
 

Author Comment

by:babak62
ID: 34922062
Thanks Terry for the response would you please tell me where should I incorporate this too
 $rss_channel["ITEMS"][$i]["DESCRIPTION"]  
0
 

Author Comment

by:babak62
ID: 34922080
Sorry for my ignorance I did it like this
$sourcestring= $rss_channel["ITEMS"][$i]["DESCRIPTION"];
preg_match_all('/img[^>]*src="[^"]*"/',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);

and this is the output


Array
(
    [0] => Array
        (
            [0] => img src="http://www.w3wallpapers.com/thumbs/star_trails_mount_rainier_washington___1600x12-t2.jpg"
        )

)
 

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:babak62
ID: 34922127
Treey;
Is ths supposed to print all the records or only one of the recoreds in the array
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34922336
Did you want the array values updated, or just printed? Presumably $i is a variable number, in which case you'd do something like (pseudocode):

for each value of $i {
  $sourcestring= $rss_channel["ITEMS"][$i]["DESCRIPTION"];
  preg_match_all('/img[^>]*src="[^"]*"/',$sourcestring,$matches);
  print $matches[0];
}

or:
# Update the image tags:
for each value of $i {
  $rss_channel["ITEMS"][$i]["DESCRIPTION"] = preg_replace('/.*(img[^>]*src="[^"]*").*/',"$1", $rss_channel["ITEMS"][$i]["DESCRIPTION"]);
}
0
 

Author Comment

by:babak62
ID: 34922368
One more thing I need to ask you and that is my fault I need the tag without the  scr= and "" at the bgining and the end also
http://www.somesite.com/thumbs/latourell_falls_columbia_river_oregon___1600x1-t2.jpg
I did try the app you used but could not get this out would you PLEASR try this one more time and get me the results.  Whaat I need to do later is to use these URLs for either a slideshow or sonething like that and for that I only need the url to put it in the imgage tag later.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34922415
This should do it:

preg_match_all('/img[^>]*src="([^"]*)"/',$sourcestring,$matches);
print $matches[1];
0
 

Author Comment

by:babak62
ID: 34922444
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 34922481
This works for me:

$string = '<p><a href="http://www.somesite.com/latourell_falls_columbia_river_oregon___1600x1-w
allpapers.html" ><img src="http://www.somesite.com/thumbs/latourell_falls_columbia_river_oregon
___1600x1-t2.jpg" align="left" /></a></p><br clear=all>';

preg_match_all('/img[^>]*src\s*=\s*"([^"]*)"/i',$string,$matches);
print $matches[1][0];
0
 

Author Closing Comment

by:babak62
ID: 34922525
PERFECT ANSWER MILLION THANKS
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 …

679 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