PHP validation/regex for URL

后端 未结 21 2108
青春惊慌失措
青春惊慌失措 2020-11-22 01:19

I\'ve been looking for a simple regex for URLs, does anybody have one handy that works well? I didn\'t find one with the zend framework validation classes and have seen sev

21条回答
  •  心在旅途
    2020-11-22 02:08

    Here is the way I did it. But I want to mentoin that I am not so shure about the regex. But It should work thou :)

    $pattern = "#((http|https)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|”|\"|'|:|\<|$|\.\s)#i";
            $text = preg_replace_callback($pattern,function($m){
                    return "$m[1]$m[4]";
                },
                $text);
    

    This way you won't need the eval marker on your pattern.

    Hope it helps :)

提交回复
热议问题