Any preg_match() to extract image urls from text?

后端 未结 3 701
半阙折子戏
半阙折子戏 2020-12-06 03:19

i need a preg_match() syntax or something similar to extract JPG or PNG or GIF URLs from a m

相关标签:
3条回答
  • 2020-12-06 04:00

    Update for the case there is prefix http/https optional, example:

    http://example.com/image.jpg
    https://example.com/image.jpg
    //example.com/image.jpg
    
    function extractImageUrlFromText($text) {
      preg_match_all('!(https?:)?//\S+\.(?:jpe?g|jpg|png|gif)!Ui', 
      $text, $matches);
      return $matches[0];
    }
    
    0 讨论(0)
  • 2020-12-06 04:10

    Please note the special occasions where they can fool your server inserting fake matches.

    For example:

    http://www.myserver.com/virus.exe?fakeParam=.jpg
    

    Or

    http://www.myserver.com/virus.exe#fakeParam=.jpg
    

    I've modified quickly the regex to avoid this cases, but i'm pretty sure there could be more (like inserting %00 in the path of the file, for example, and cannot be easily parsed by a regex)

    $matches = array();
    preg_match_all('!http://[^?#]+\.(?:jpe?g|png|gif)!Ui' , $string , $matches);
    

    So, for security, use always regex in the most restrictive way, for example, if you know the server, write it into the regex, or if you know that the path always will include letters, hyphens, dots, slashes and numbers, use one expression like:

    $matches = array();
    preg_match_all('!http://[a-z0-9\-\.\/]+\.(?:jpe?g|png|gif)!Ui' , $string , $matches);
    

    This should avoid any funny surprise in the future.

    0 讨论(0)
  • 2020-12-06 04:11
    $matches = array();
    preg_match_all('!http://.+\.(?:jpe?g|png|gif)!Ui' , $string , $matches);
    
    0 讨论(0)
提交回复
热议问题