@@ -55,3 +55,90 @@ function get_headers(string $url, int $format = 0, $context = null): array
55
55
}
56
56
return $ result ;
57
57
}
58
+
59
+
60
+ /**
61
+ * This function parses a URL and returns an associative array containing any
62
+ * of the various components of the URL that are present.
63
+ * The values of the array elements are not URL decoded.
64
+ *
65
+ * This function is not meant to validate
66
+ * the given URL, it only breaks it up into the above listed parts. Partial
67
+ * URLs are also accepted, parse_url tries its best to
68
+ * parse them correctly.
69
+ *
70
+ * @param string $url The URL to parse. Invalid characters are replaced by
71
+ * _.
72
+ * @param int $component Specify one of PHP_URL_SCHEME,
73
+ * PHP_URL_HOST, PHP_URL_PORT,
74
+ * PHP_URL_USER, PHP_URL_PASS,
75
+ * PHP_URL_PATH, PHP_URL_QUERY
76
+ * or PHP_URL_FRAGMENT to retrieve just a specific
77
+ * URL component as a string (except when
78
+ * PHP_URL_PORT is given, in which case the return
79
+ * value will be an integer).
80
+ * @return mixed On seriously malformed URLs, parse_url.
81
+ *
82
+ * If the component parameter is omitted, an
83
+ * associative array is returned. At least one element will be
84
+ * present within the array. Potential keys within this array are:
85
+ *
86
+ *
87
+ *
88
+ * scheme - e.g. http
89
+ *
90
+ *
91
+ *
92
+ *
93
+ * host
94
+ *
95
+ *
96
+ *
97
+ *
98
+ * port
99
+ *
100
+ *
101
+ *
102
+ *
103
+ * user
104
+ *
105
+ *
106
+ *
107
+ *
108
+ * pass
109
+ *
110
+ *
111
+ *
112
+ *
113
+ * path
114
+ *
115
+ *
116
+ *
117
+ *
118
+ * query - after the question mark ?
119
+ *
120
+ *
121
+ *
122
+ *
123
+ * fragment - after the hashmark #
124
+ *
125
+ *
126
+ *
127
+ *
128
+ * If the component parameter is specified,
129
+ * parse_url returns a string (or an
130
+ * integer, in the case of PHP_URL_PORT)
131
+ * instead of an array. If the requested component doesn't exist
132
+ * within the given URL, NULL will be returned.
133
+ * @throws UrlException
134
+ *
135
+ */
136
+ function parse_url (string $ url , int $ component = -1 )
137
+ {
138
+ error_clear_last ();
139
+ $ result = \parse_url ($ url , $ component );
140
+ if ($ result === false ) {
141
+ throw UrlException::createFromPhpError ();
142
+ }
143
+ return $ result ;
144
+ }
0 commit comments