Solved

Extract IMG from code

Posted on 2011-02-17
10
839 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 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