-
-
Save vyspiansky/11285153 to your computer and use it in GitHub Desktop.
<?php | |
// Source: http://goo.gl/qyLFbg | |
$html = '<img border="0" src="/images/image.jpg" alt="Image" width="100" height="100" />'; | |
preg_match( '@src="([^"]+)"@' , $html, $match ); | |
$src = array_pop($match); | |
// will return /images/image.jpg | |
echo $src; |
Hi,
If my html is like this $html = "<img border="0" src="/images/image1.jpg" alt="Image" width="100" height="100" /><img border="0" src="/images/image2.jpg" alt="Image" width="100" height="100" /><img border="0" src="/images/image3.jpg" alt="Image" width="100" height="100" />";
Then how can i get the all image tag src
Thanks
KamleshPal
You can use the preg_match_all function. For instance:
$html = '
<img border="0" src="/images/image1.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image2.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image3.jpg" alt="Image" width="100" height="100" />
';
preg_match_all( '@src="([^"]+)"@' , $html, $match );
$src = array_pop($match);
print_r($src);
Output:
Array (
[0] => /images/image1.jpg
[1] => /images/image2.jpg
[2] => /images/image3.jpg
)
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $body, $result);
preg_match('~<img.*?src=["\']+(.*?)["\']+~', $body, $result);
Hy, in my case i have a html file which has store the view source of url and then i want to Get all 'img' tages with 'alt' from html file ,so what i do for this....
thanks in advance.
Hy, in my case i have a html file which has store the view source of url and then i want to Get all 'img' tages with 'alt' from html file ,so what i do for this....
thanks in advance.
$html = '
<img border="0" src="/images/image1.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image2.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image3.jpg" alt="Image" width="100" height="100" />
';
$get_Img_Src = '/<img[^>]*src=([\'"])(?<src>.+?)\1[^>]*>/i'; //for get img src path only...
preg_match_all($get_Img_Src, $html, $result);
echo $result['src'][0];
echo $result['src'][1];
OR
$get_Img_Src = '/<img[^>]*src=([\'"])(?<src>.+?)\1[^>]*alt=([\'"])(?<alt>.+?)\2*>/i'; //for get img src path & alt text also
preg_match_all($get_Img_Src, $html, $result);
echo $result['src'][0];
echo $result['src'][1];
echo $result['alt'][0];
echo $result['alt'][1];
Hola, es mi caso funciono a la perfección, gracias. Saludos.
preg_match_all('/<img[^>]+>/i',$text, $result);