Regular expression to find URLs within a string

前端 未结 27 2122
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 14:18

Does anyone know of a regular expression I could use to find URLs within a string? I\'ve found a lot of regular expressions on Google for determining if an entire string is

27条回答
  •  生来不讨喜
    2020-11-22 14:50

    I think this regex pattern handle precisely what you want

    /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/
    

    and this is an snippet example to extract Urls:

    // The Regular Expression filter
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    
    // The Text you want to filter for urls
    $text = "The text you want  https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string to filter goes here.";
    
    // Check if there is a url in the text
    preg_match_all($reg_exUrl, $text, $url,$matches);
    var_dump($matches);
    

提交回复
热议问题