正则表达式解析长网址

时间:2011-08-07 07:22:58

标签: php regex url

我有这个正则表达式,效果很好,但并非在所有情况下,例如,如果我有一个长网址说“http://www.gob.cl/especiales/politicas-y-propuestas-de-accion-para- el-desarrollo-de-la-educacion-chilena /“它只会归还我”http://www.gob。“作为网址的一部分

这是我的代码

    $regexUrl = "((https?|ftp)\:\/\/)?"; // SCHEME 
    $regexUrl .= "([a-zA-Z0-9+!*(),;?&=\$_.-]+(\:[a-zA-Z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass 
    $regexUrl .= "([a-zA-Z0-9-]+)\.([a-zA-Z]{2,3})";  // Host or IP 
    $regexUrl .= "(\:[0-9]{2,5})?"; // Port 
    $regexUrl .= "(\/([a-zA-Z0-9+\$_-]\.?)+)*\/?"; // Path 
    $regexUrl .= "(\?[a-zA-Z+&\$_.-][a-zA-Z0-9;:@&%=+\/\$_.-]*)?"; // GET Query 
    $regexUrl .= "(#[a-zA-Z_.-][a-zA-Z0-9+\$_.-]*)?"; // Anchor 
    //if(preg_match_all("#\bhttps?://[^\s()]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#", $message, $matches1, PREG_PATTERN_ORDER))
    //$pattern = '/((https?|ftp)\:(\/\/)|(file\:\/{2,3}))?(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(((([a-zA-Z0-9]+)(\.)?)+)(\.)(com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|[a-z]{2}))([\/][\/a-zA-Z0-9\.]*)*([\/]?(([\?][a-zA-Z0-9]+[\=][a-zA-Z0-9\%\(\)]*)([\&][a-zA-Z0-9]+[\=][a-zA-Z0-9\%\(\)]*)*))?/';
    if(preg_match_all("/$regexUrl/", $urlMessage, $matches1, PREG_PATTERN_ORDER))
    {
      try
        {
            foreach($matches1[0] as $urlToTrim1)
            {
                $url= $urlToTrim1;
                echo $url;
            }
        }
        catch(Exception $e)
        {
            $url="-1";
        }
    }

是否可以使用通用正则表达式来解析所有类型的URL。

3 个答案:

答案 0 :(得分:2)

正则表达式的主机或IP部分

"([a-zA-Z0-9-]+)\.([a-zA-Z]{2,3})"

允许内部最多一个点。因此“www.xyz.com”永远不会匹配。

我不知道你的具体要求是什么,但你可能会选择

"([^/?#:]+)"

为主持人部分。

答案 1 :(得分:1)

以下是我从$ text中提取所有网址的内容:

preg_match_all('#(https?://[a-z0-9\.\-_\#%&=/?;,!:~@\$\+]+)#iu', $text, $m);

假设URL以https://或http://开头,然后可以将序列限制为一组特殊的字符。

然后我可以$m使用http://www.php.net/manual/en/function.parse-url.php来获取所有网址的详细信息

编辑:此外,如果要解析文本,您可能需要检查URL末尾的句点(.)和其他标点符号。我注意到,如果网址位于句子末尾,人类可能会在网址末尾放置.,例如http://example.com

所以我这样做:

 if (($url[$pos]==='!') || ($url[$pos]==='.')) { // probably do not want these chars at the end of a url!
    $url = substr($url, 0, $pos);
 }

答案 2 :(得分:-1)

如何匹配以http://或https://

开头的所有内容

((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))