In a complete url address passed by the variable, we need to obtain the domain name in the address. here we can use the parse_url function for processing. Of course, some users can use regular expressions to solve this problem, the following are two examples. In a complete url address passed by the variable, we need to obtain the domain name in the address. here we can use the parse_url function for processing. Of course, some users can use regular expressions to solve this problem, the following are two examples.
Script ec (2); script
Method 1php built-in function (parse_url)
The Code is as follows: |
|
$ Url = 'HTTP: // www.111cn.net/index.html '; $ Tempu = parse_url ($ Url ); $ Message = $ tempu ['host']; Echo $ message; // The output result is www.111cn.net. |
Method 2 User-Defined Functions
The Code is as follows: |
|
Function get_domain ($ url) { $ Pattern = "/[/w-] + /. (com | net | org | gov | biz | com.tw | com.hk | com.ru | net.tw | net.hk | net.ru | info | cn | com.cn | net.cn | org.cn | gov.cn | mobi | name | sh | ac | la | travel | tm | us | cc | TV | jobs | asia | hn | lc | hk | bz | com.hk | ws | tel | io | tw | ac.cn | bj.cn | region | tj.cn | cq.cn | he.cn | sx.cn | nm.cn | ln.cn | jl.cn | hl.cn | js.cn | zj.cn | ah.cn | fj.cn | jx.cn | sd.cn | ha.cn | region | | hi.cn | SC .cn | region | yn.cn | xz.cn | sn.cn | gs.cn | qh.cn | nx.cn | xj.cn | tw.cn | hk.cn | mo.cn | org.hk | is | edu | mil | au | jp | int | kr | de | vc | ag | in | me | edu.cn | co. kr | gd | vg | co. uk | be | sg | it | ro | com.mo )(/. (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"); // 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; } } } |