簡體   English   中英

PHP URL驗證

[英]PHP URL validation

我知道有無數個線程在問這個問題,但是我一直找不到能夠幫助我解決這個問題的線程。

我基本上是在嘗試解析大約10,000,000個URL的列表,確保它們按照以下條件有效,然后獲取根域URL。 該列表幾乎包含了您可以想象的所有內容,包括類似的東西(以及預期的格式化網址):

biy.ly/test [VALID] [return - bit.ly]
example.com/apples?test=1&id=4 [VALID] [return - example.com]
host101.wow404.apples.test.com/cert/blah [VALID] [return - test.com]
101.121.44.xxx [**inVALID**] [return false]
localhost/noway [**inVALID**] [return false]
www.awesome.com [VALID] [return - awesome.com]
i am so awesome [**inVALID**] [return false]
http://404.mynewsite.com/visits/page/view/1/ [VALID] [return - mynewsite.com]
www1.151.com/searchresults [VALID] [return - 151.com]

有人對此有任何建議嗎?

^(?:https?://)?(?:[a-z0-9-]+\.)*((?:[a-z0-9-]+\.)[a-z]+)

說明

^                # start-of-line
(?:              # begin non-capturing group
  https?         #   "http" or "https"
  ://            #   "://"
)?               # end non-capturing group, make optional
(?:              # start non-capturing group
  [a-z0-9-]+\.   #   a name part (numbers, ASCII letters, dashes) & a dot
)*               # end non-capturing group, match as often as possible
(                # begin group 1 (this will be the domain name)
  (?:            #   start non-capturing group
    [a-z0-9-]+\. #     a name part, same as above
  )              #   end non-capturing group
  [a-z]+         #   the TLD
)                # end group 1 

http://rubular.com/r/g6s9bQpNnC

我將從默認值開始:

filter_var($inputUrl, FILTER_VALIDATE_URL);

然后添加您無法進一步驗證的特殊情況。 這應該簡化一點。

至於獲得主持人。

parse_url($inputUrl, PHP_URL_HOST);

^(([a-zA-Z](\\.[a-zA-Z])+)|([0-9]{1,3}(\\.[0-9]{1,3}){3})/.*$

編輯

在php中將是preg_match ( '^(([a-zA-Z](\\.[a-zA-Z])+)|([0-9]{1,3}(\\.[0-9]{1,3}){3})/.*$' , $myUrls , $matches)

您需要的是$matches[1]

$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$w$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
  {
  $websiteErr = "Invalid URL";
  }ebsite))
  {
  $websiteErr = "Invalid URL";
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM