首页  >  问答  >  正文

php - 如何取出下面字符串中的多个src路径

取出image/之后的20170626/1498445461112119.png(日期/文件名)路径也可以,前面的我可以手动拼。。。

很抱歉不能采纳多个,我是取出来后才登陆segmentfault的,非常感谢各位。

附正则:$preg='//d/d.[jpg|jpeg|png|bmp|gif]*/i';

黄舟黄舟2646 天前923

全部回复(5)我来回复

  • PHP中文网

    PHP中文网2017-06-27 09:20:07

    <?php
    $str = '<p>图1<img src="/public/upload/ueditor/image/20170626/1234567890.png" title="1234567890.png" alt="QQ图片1234567890.png"></p><p>图1<img src="/public/upload/ueditor/image/20170626/0987654321.png" title="0987654321.png" alt="QQ图片0987654321.png"></p>';
    
    if (preg_match_all('/src="(.+?)"/', $str, $matches)) {
        print_r($matches);
    } else {
        echo '没有匹配';
    }

    输出:

    (
        [0] => Array
            (
                [0] => src="/public/upload/ueditor/image/20170626/1234567890.png"
                [1] => src="/public/upload/ueditor/image/20170626/0987654321.png"
            )
    
        [1] => Array
            (
                [0] => /public/upload/ueditor/image/20170626/1234567890.png
                [1] => /public/upload/ueditor/image/20170626/0987654321.png
            )
    
    )

    感兴趣欢迎看我的正则表达式入门讲堂: 一个小时学会使用正则表达式

    回复
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-27 09:20:07

    正则表达式比较方便

    回复
    0
  • 高洛峰

    高洛峰2017-06-27 09:20:07

    正则匹配下,然后要取的那块用()当子集取出来就可以了

    回复
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-27 09:20:07

    雷雷

    回复
    0
  • 学习ing

    学习ing2017-06-27 09:20:07

    <?php
    define('CLI_SCRIPT', true);
    
    $tt = '<p><img class="icon" alt="141220154.zip" title="141220154.zip" src="http://11111/theme/image.php/ gourmet/core/1491882323/f/archive" /> </p><p><img class="icon" alt="141220154.zip" title="141220154.zip" src="http://3333/theme/image.php/gourmet/core/         1491882323/f/archive" /></p>';
    
    preg_match_all('/<img [^>]*src=\"(.+?)\"/', $tt, $matches);                                                                                                                 
    var_dump($matches);    // 需要的字符串在$matches[1]中,具体信息可以打印了看

    参考link: http://php.net/manual/zh/func...

    回复
    0
  • 取消回复