V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
jookr
V2EX  ›  问与答

php 获取图片链接的正则表达式 多帮我获取了一个冒号 百思不得其解求解答

  •  
  •   jookr · 2015-03-25 12:47:40 +08:00 · 2354 次点击
    这是一个创建于 3291 天前的主题,其中的信息可能已经有所发展或是发生改变。

    '''php
    //先定义一个获取所有图片的函数
    function getSrc($content){

    $pattern="/<[img|IMG].*?src=[\\\"|\\\'|\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg|\.bmp]))[\\\"|\\\'|\"|\'].*?[\/]?>/i";
    preg_match_all($pattern,$content,$match);
    return $match;
    

    }
    //字符串
    $str = ' <img src=\"http://127.0.0.1/file/upload/201503/24/1.jpg\" width=\"554\" height=\"416\" alt=\"\" />
    <img src="http://127.0.0.1/file/upload/201503/24/2.jpg" width=\"550\" height=\"775\" alt=\"\" />
    <img src="http://127.0.0.1/file/upload/201503/24/3.jpg" width=\"336\" height=\"508\" alt=\"\" />';

    //获取并且输出
    print_r(getSrc($str));

    //输出内容如下
    /*

    Array
    (
    [0] => Array
    (
    [0] => <img src=\"http://127.0.0.1/file/upload/201503/24/1.jpg\" width=\"554\" height=\"416\" alt=\"\" />
    [1] => <img src="http://127.0.0.1/file/upload/201503/24/2.jpg" width=\"550\" height=\"775\" alt=\"\" />
    [2] => <img src="http://127.0.0.1/file/upload/201503/24/3.jpg" width=\"336\" height=\"508\" alt=\"\" />
    )

    [1] => Array
        (
            [0] => "http://127.0.0.1/file/upload/201503/24/1.jpg
            [1] => http://127.0.0.1/file/upload/201503/24/2.jpg
            [2] => http://127.0.0.1/file/upload/201503/24/3.jpg
        )
    

    )
    //为啥$str[1][0]开头的地方会带双引号呢?怎么写才能让它不带双引号
    */

    5 条回复    2015-03-25 13:27:03 +08:00
    lianyue
        1
    lianyue  
       2015-03-25 12:56:32 +08:00
    [\\\"|\\\'|\'|\"]
    换成 () 括号 (\\\"|\\\'|\'|\")
    imn1
        2
    imn1  
       2015-03-25 13:04:59 +08:00
    @lianyue
    他几乎把全部或选择的都写成[]了,能出这个结果已经算是撞大运了
    lianyue
        3
    lianyue  
       2015-03-25 13:20:31 +08:00
    @imn1 我只注意到了 多余的部分
    kungfuchicken
        4
    kungfuchicken  
       2015-03-25 13:25:53 +08:00
    <img.*?src=\\?(?:\'|\")(.*?\.(?:gif|jpg|png|jpeg|bmp]))\\?(?:\'|\").*?\/?>
    kungfuchicken
        5
    kungfuchicken  
       2015-03-25 13:27:03 +08:00
    <img.*?src=\\?(?:\'|\")(.*?\.(?:gif|jpg|png|jpeg|bmp))\\?(?:\'|\").*?\/?>
    更正一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3493 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:54 · PVG 18:54 · LAX 03:54 · JFK 06:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.