search
Homephp教程PHP源码PHP提取字符串中的图片地址2种方法

文章有两个正则匹配图片的例子,头一个是获取文章中所有图片地址出来,第二个例子就是只要以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;
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.