Solved

Extract IMG from code

Posted on 2011-02-17
10
834 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

825 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