Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Extract IMG from code

Posted on 2011-02-17
10
Medium Priority
?
842 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 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

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!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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

824 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