Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于php正则表达式的Url检测算法_Php_Regex - Fatal编程技术网

基于php正则表达式的Url检测算法

基于php正则表达式的Url检测算法,php,regex,Php,Regex,我想实现一个url匹配算法,检测以http或https开头的每个url。我提出了一个解决方案,但我不确定是否只考虑空间和控制字符 function func($str){ $url = '/((http|https)[^[:space:][:cntrl:]]+)/i'; $str = nl2br(htmlspecialchars($str)); return preg_replace($url, '<a href="$1" target="_blank">$1

我想实现一个url匹配算法,检测以http或https开头的每个url。我提出了一个解决方案,但我不确定是否只考虑空间和控制字符

function func($str){
    $url = '/((http|https)[^[:space:][:cntrl:]]+)/i';
    $str = nl2br(htmlspecialchars($str));
    return preg_replace($url, '<a href="$1" target="_blank">$1</a>', $str);
}
函数func($str){ $url='/((http | https)[^[:space:][:cntrl:][]+)/i'; $str=nl2br(htmlspecialchars($str)); 返回preg_replace($url,,$str); } 类似这样的内容:


function url_link($str) {
    if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches)) {
        $target = " target=\"_blank\" ";

        for ($i = 0; $i < count($matches['0']); $i++) {
            $period = '';
            if (preg_match("|\.$|", $matches['6'][$i])) {
                $period = '.';
                $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
            }

            $str = str_replace($matches['0'][$i], $matches['1'][$i] . '<a href="http' .
                    $matches['4'][$i] . '://' .
                    $matches['5'][$i] .
                    $matches['6'][$i] . '"' . $target . '>http' .
                    $matches['4'][$i] . '://' .
                    $matches['5'][$i] .
                    $matches['6'][$i] . '</a>' .
                    $period, $str);
        }
    }
    return $str;
}

函数url\u链接($str){
如果(preg|u match|u all(“#(^ |\s |\()((http(s?)/)|(www\)))(\w+[^\s\)\Google是你的朋友!你应该更经常地使用它:)重复?可能的重复