PHP validation/regex for URL

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

    I used this on a few projects, I don't believe I've run into issues, but I'm sure it's not exhaustive:

    $text = preg_replace(
      '#((https?|ftp)://(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i',
      "'$3$4'",
      $text
    );
    

    Most of the random junk at the end is to deal with situations like http://domain.com. in a sentence (to avoid matching the trailing period). I'm sure it could be cleaned up but since it worked. I've more or less just copied it over from project to project.

提交回复
热议问题