Solved

Extract IMG from code

Posted on 2011-02-17
10
831 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

705 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

13 Experts available now in Live!

Get 1:1 Help Now