The PHP regular expression retrieves the domain name from the url. The following code obtains the domain name from the URL, for example, liehuo.netliveq.html. the following liehuo.net references: functionget_domain ($ url) {$ pattern [w-] +. (com | net | org the following code can get the domain name from the URL
Example: http://bkjia.com/live/q.html
Obtain bkjia.com.
Reference content is as follows:
Function get_domain ($ url ){
$ Pattern = "/[\ w-] + \. (com | net | org | gov | cc | biz | info | cn )(\. (cn | hk ))*/";
Preg_match ($ pattern, $ url, $ matches );
If (count ($ matches)> 0 ){
Return $ matches [0];
} Else {
$ Rs = parse_url ($ url );
$ Main_url = $ rs ["host"];
If (! Strcmp (long2ip (sprintf ("% u", ip2long ($ main_url), $ main_url )){
Return $ main_url;
} Else {
$ Arr = explode (".", $ main_url );
$ Count = count ($ arr );
$ EndArr = array ("com", "net", "org", "3322"); // com.cn net.cn
If (in_array ($ arr [$ count-2], $ endArr )){
$ Domain = $ arr [$ count-3]. ".". $ arr [$ count-2]. ".". $ arr [$ count-1];
} Else {
$ Domain = $ arr [$ count-2]. ".". $ arr [$ count-1];
}
Return $ domain;
} // End if (! Strcmp ...)
} // End if (count ...)
} // End function |
Example: the http://liehuo.net/live/q.html to get liehuo.net is referenced below: function get_domain ($ url) {$ pattern =/[\ w-] + \. (com | net | org...