PHP: 正则提取文章中所有图片地址, get image src attribute (regular expression)

 

<?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;

 

大段文章的话,使用:

<?php
$content = '{lots of text and lots of images}';
preg_match_all('@img\s*src="([^"]+)"@',$content, $result);
var_dump($result);

 

PHP: 正则提取文章中所有图片地址, get image src attribute (regular expression)
PHP: 正则提取文章中所有图片地址, get image src attribute (regular expression)
PHP: 正则提取文章中所有图片地址, get image src attribute (regular expression)
PHP: 正则提取文章中所有图片地址, get image src attribute (regular expression)

 

 

 

本文:PHP: 正则提取文章中所有图片地址, get image src attribute (regular expression)

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.