Better way to validate a URL in PHP

前端 未结 2 1378
迷失自我
迷失自我 2021-01-15 05:20

I\'ve been reading various posts on Stack Overflow to try and find an ideal way to validate a URL in PHP. My research has come up with three possible solutions, however, no

相关标签:
2条回答
  • 2021-01-15 06:02

    How about you combine filter_var (which basically is a regex check that spans pages) with additional regex check for cases you don't think are covered well by it?

    0 讨论(0)
  • 2021-01-15 06:17

    I ussually use parse_url() for check/validate url (in most case, i need to determine whether the url is already a valid url or a relative url)

    if (parse_url($url, PHP_URL_SCHEME) != '')
    {
       // Url is absolute
    }
    else
    {
       // Url is not an absolute url, and therefore, need more validation...
    }
    
    0 讨论(0)
提交回复
热议问题