PHP validation/regex for URL

后端 未结 21 2425
青春惊慌失措
青春惊慌失措 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 01:58

    I don't think that using regular expressions is a smart thing to do in this case. It is impossible to match all of the possibilities and even if you did, there is still a chance that url simply doesn't exist.

    Here is a very simple way to test if url actually exists and is readable :

    if (preg_match("#^https?://.+#", $link) and @fopen($link,"r")) echo "OK";
    

    (if there is no preg_match then this would also validate all filenames on your server)

提交回复
热议问题