View Single Post
Old 08-04-2009, 10:55 AM   #10 (permalink)
sketchMedia
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

To get the img src you could use something like this, now baring in mind i'm not the best at regex, so i'm sure Salathe will post with improvements!
PHP Code:
<?php
$pattern 
'~<img(?:.*)src="(.+?)"(?:.*)\/>~i';
$subject '<div id="images"><img alt="awd" src="http://www.jaoudestudios.com/images/image1.jpg" alt="This is an image" /></div>';
preg_match($pattern$subject$matches);

echo 
$matches[1];
Now, to match all img src, you need to do thus:
PHP Code:
<?php
$pattern 
'~<img(?:.*)src="(.+?)"(?:.*)\/>~i';
$subject '<div id="images"><img alt="awd" src="http://www.jaoudestudios.com/images/image1.jpg" alt="This is an image" /><img alt="awd" src="http://www.jaoudestudios.com/images/image1.jpg" alt="This is an image" /><img alt="awd" src="http://www.jaoudestudios.com/images/image1.jpg" alt="This is an image" /><img alt="awd" src="http://www.jaoudestudios.com/images/imagawdawd1.jpg" alt="This is an image" /><img alt="awd" src="http://www.jaoudestudios.com/images/iawdawd1.jpg" alt="This is an image" /></div>';
preg_match_all($pattern$subject$matches);

foreach(
$matches[1] as $match)
{
     echo 
'<br />' $match;
}
Alternitivly, you could use xpath + domdocument:

PHP Code:
$subject '<div id="images"><img alt="awd" src="http://www.jaoudestudios.com/images/image1.jpg" alt="This is an image" /><img alt="awd" src="http://www.jaoudestudios.com/images/image1.jpg" alt="This is an image" /><img alt="awd" src="http://www.jaoudestudios.com/images/image1.jpg" alt="This is an image" /><img alt="awd" src="http://www.jaoudestudios.com/images/imagawdawd1.jpg" alt="This is an image" /><img alt="awd" src="http://www.jaoudestudios.com/images/iawdawd1.jpg" alt="This is an image" /></div>';
$pDom = new DOMDocument();
libxml_use_internal_errors(true);
$pDom->loadHTML($subject);
libxml_use_internal_errors(false);

$pXPath = new DOMXPath($pDom);
$pImgs$pXPath->evaluate("//img");


foreach(
$pImgs as $pImg)
{
    echo 
'<br />' $pImg->getAttribute('src');

__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote