0

I’m using Magepierss to extract feeds news from some urls i get the title, the date, etc.. But not the image, it’s exist in this fields [description] within a html code

[description] => <div style="margin: 5px 5% 10px 5%;"><img src="http://www.supermotoaustralia.com/wp-content/uploads/2014/07/showroom.jpg" width="90%" /></div><div>Australians purchased 53,396 new motorcycles, all-terrain vehicles (ATVs) and scooters in the first half of 2014, with road motorcycle purchases making up 40.6 per cent of these sales. While sales in the road motorcycle segment were up 2.2 per cent, total motorcycle, ATV and scooter sales were down 0.5 per cent compared to the same [&#8230;]</div>

and i want to do a function in php to exctrat just the image frome this html code any ideas ?

karim karim
  • 81
  • 1
  • 1
  • 11
  • yes if it is possible – karim karim Jul 24 '14 at 14:44
  • @karimkarim: It sounds like what you're looking for is called a "DOM parser." You would use that to extract specific elements/attributes/values/etc. from an HTML document string. – David Jul 24 '14 at 14:47
  • 1
    @castor: -infinity for you. regexes on html is a stupid idea, and using ereg is even stupider. ereg has been deprecated for years. – Marc B Jul 24 '14 at 14:47
  • 1
    @castor: Obligatory: http://stackoverflow.com/a/1732454/328193 – David Jul 24 '14 at 14:47
  • You are right, I have confused eregi() with preg_march() - forgot which one is deprecated and didn't read it on the php site. – castor Jul 24 '14 at 14:50

4 Answers4

3

Or just as an alternative, in case you would consider an OOP approach instead of the procedural one, you can easily use DomDocument class to navigate through the provided DOM and utilize its useful methods.

An example:

$str = '<div style="margin: 5px 5% 10px 5%;"><img src="http://www.supermotoaustralia.com/wp-content/uploads/2014/07/showroom.jpg" width="90%" /></div><div>Australians purchased 53,396 new motorcycles, all-terrain vehicles (ATVs) and scooters in the first half of 2014, with road motorcycle purchases making up 40.6 per cent of these sales. While sales in the road motorcycle segment were up 2.2 per cent, total motorcycle, ATV and scooter sales were down 0.5 per cent compared to the same [&#8230;]</div>';

$dom = new DomDocument();
$dom->loadHTML($str);

$imgs = $dom->getElementsByTagName("img");

foreach ($imgs as $img) 
{
    var_dump($img->getAttribute('src'));
}
ilpaijin
  • 3,645
  • 2
  • 23
  • 26
1

You need to use regex.Try this :

$str = '<div style="margin: 5px 5% 10px 5%;"><img src="http://www.supermotoaustralia.com/wp-content/uploads/2014/07/showroom.jpg" width="90%" /></div><div>Australians purchased 53,396 new motorcycles, all-terrain vehicles (ATVs) and scooters in the first half of 2014, with road motorcycle purchases making up 40.6 per cent of these sales. While sales in the road motorcycle segment were up 2.2 per cent, total motorcycle, ATV and scooter sales were down 0.5 per cent compared to the same [&#8230;]</div>';


$rx_main = '(<img src="(.+)")Ui';
preg_match($rx_main,$str,$result); 

print_r($result[1]);// will produce http://www.supermotoaustralia.com/wp-content/uploads/2014/07/showroom.jpg
Harshal
  • 3,562
  • 9
  • 36
  • 65
0
function get_srcs ( $string ) {

    static $matches;
    preg_match_all( '/<img.*?src.*?=.*?(?:\'")(.*?)\1/', $string, $matches );
    return array_column( $matches , 1 );

}
Vlas Bashynskyi
  • 1,886
  • 2
  • 16
  • 25
0

create regular expression and use preg_match() PHP function to extract it.