>  기사  >  php教程  >  PHP提取字符串中的图片地址2种方法

PHP提取字符串中的图片地址2种方法

WBOY
WBOY원래의
2016-06-08 17:22:081388검색

文章有两个正则匹配图片的例子,头一个是获取文章中所有图片地址出来,第二个例子就是只要以http,https打开的图片地址,第二个例子多半用来采集远程图片,第一个例子多半用来获取字符串中图片地址或提取第一张图片。

<script>ec(2);</script>

例子1 获取字符串中所有图片

$str='

';
$pattern="//";
preg_match_all($pattern,$str,$match);
print_r($match);
?>

结果显示:

Array
(
[0] => Array
(
[0] => ””/
)

[1] => Array
(
[0] => upfiles/2009/07/1246430143_1.jpg
)

)

盒例子2,这个函数是提取站外以http,https

/**
* 提取字符串中图片url地址
* @param type $str
* @return type
*/
function getimgs($str) {
    $reg = '/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(jpg|gif|png)/';
    $matches = array();
    preg_match_all($reg, $str, $matches);
    foreach ($matches[0] as $value) {
        $data[] = get_file($value);
    }
    return $data;
}

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.