88
99namespace inhere \librarys \helpers ;
1010
11- use inhere \librarys \traits \TraitUrlHelper ;
12-
1311class UrlHelper
1412{
15- use TraitUrlHelper;
13+ public static function isUrl ($ str )
14+ {
15+ $ rule = '/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i ' ;
16+
17+ return preg_match ($ rule ,$ str )===1 ;
18+ }
19+
20+ public static function canAccessed ($ url )
21+ {
22+ $ url = trim ($ url );
23+
24+ if ( function_exists ('curl_init ' ) ) {
25+ // use curl
26+ $ ch = curl_init ($ url );
27+ curl_setopt ($ ch , CURLOPT_NOBODY , true );
28+ curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 5 );//设置超时
29+ curl_setopt ($ ch , CURLOPT_TIMEOUT , 5 );
30+
31+ if ( false !== curl_exec ($ ch ) ){
32+ $ statusCode = (int )curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
33+
34+ return $ statusCode === 200 ;
35+ }
36+ } elseif ( function_exists ('get_headers ' ) ) {
37+ $ headers = get_headers ($ url , 1 );
38+
39+ return (bool )preg_match ('/200/ ' ,$ headers [0 ]);
40+ } else {
41+ $ opts = [
42+ 'http ' => ['timeout ' => 5 ,]
43+ ];
44+ $ context = stream_context_create ($ opts );
45+ $ resource = @file_get_contents ($ url , false , $ context );
46+
47+ return (bool )$ resource ;
48+ }
49+
50+ return false ;
51+ }
52+
53+ // Build arrays of values we need to decode before parsing
54+ protected static $ entities = array (
55+ '%21 ' , '%2A ' , '%27 ' , '%28 ' , '%29 ' , '%3B ' , '%3A ' , '%40 ' , '%26 ' ,
56+ '%3D ' , '%24 ' , '%2C ' , '%2F ' , '%3F ' , '%23 ' , '%5B ' , '%5D '
57+ );
58+
59+ protected static $ replacements = array (
60+ '! ' , '* ' , "' " , '( ' , ') ' , '; ' , ': ' , '@ ' , '& ' ,
61+ '= ' , '$ ' , ', ' , '/ ' , '? ' , '# ' , '[ ' , '] '
62+ );
63+
64+ static public function parseUrl ($ url )
65+ {
66+ $ result = [];
67+
68+ // Create encoded URL with special URL characters decoded so it can be parsed
69+ // All other characters will be encoded
70+ $ encodedURL = str_replace (self ::$ entities , self ::$ replacements , urlencode ($ url ));
71+
72+ // Parse the encoded URL
73+ $ encodedParts = parse_url ($ encodedURL );
74+
75+ // Now, decode each value of the resulting array
76+ if ($ encodedParts ) {
77+ foreach ($ encodedParts as $ key => $ value ) {
78+ $ result [$ key ] = urldecode (str_replace (self ::$ replacements , self ::$ entities , $ value ));
79+ }
80+ }
81+
82+ return $ result ;
83+ }
84+
85+ /**
86+ * url_encode form urlencode(),但是 : / ? & = ...... 几个符号不会被转码为 %3A %2F %3F %26 %3D ......
87+ * $url="ftp://ud03:password@www.xxx.net/中文/中文.rar";
88+ * $url1 = url_encode1($url);
89+ * //ftp://ud03:password@www.xxx.net/%E4%B8%AD%E6%96%87/%E4%B8%AD%E6%96%87.rar
90+ * $url2 = urldecode($url);
91+ * echo $url1.PHP_EOL.$url2.PHP_EOL;
92+ * @param $url
93+ * @return mixed|string [type] [description]
94+ */
95+ static public function encode ($ url )
96+ {
97+ $ url = trim ($ url );
98+
99+ if (empty ($ url ) || !is_string ($ url ) ) {
100+ return $ url ;
101+ }
102+
103+ // 若已被编码的url,将被解码,再继续重新编码
104+ $ url = urldecode ($ url );
105+ $ encodeUrl = urlencode ($ url );
106+ $ encodeUrl = str_replace ( self ::$ entities , self ::$ replacements , $ encodeUrl );
107+
108+ return $ encodeUrl ;
109+ }
110+
111+ /**
112+ * [urlEncode 会先转换编码]
113+ * $url="ftp://ud03:password@www.xxx.net/中文/中文.rar";
114+ * $url1 = url_encode($url);
115+ * //ftp://ud03:password@www.xxx.net/%C3%A4%C2%B8%C2%AD%C3%A6%C2%96%C2%87/%C3%A4%C2%B8%C2%AD%C3%A6%C2%96%C2%87.rar
116+ * $url2 = urldecode($url);
117+ * echo $url1.PHP_EOL.$url2;
118+ * @param string $url [description]
119+ * @return mixed|string [type] [description]
120+ */
121+ static public function encode2 ($ url )
122+ {
123+ $ url = trim ($ url );
124+
125+ if (!$ url || !is_string ($ url ) ) {
126+ return $ url ;
127+ }
128+
129+ // 若已被编码的url,将被解码,再继续重新编码
130+ $ url = urldecode ($ url );
131+ $ encodeUrl = rawurlencode (mb_convert_encoding ($ url , 'utf-8 ' ));
132+
133+ // $url = rawurlencode($url);
134+
135+ $ encodeUrl = str_replace ( self ::$ entities , self ::$ replacements , $ encodeUrl );
136+
137+ return $ encodeUrl ;
138+ }
16139}
0 commit comments