<?php
$expr = '!
^
(?:
(?P<scheme> [a-z][a-z0-9.]* )
:
)?
(?:
//
(?<authority>
(?:
(?P<user> [^:@]+ )
(?:
:
(?P<pass> [^@]+ )
)?
@
)?
(?P<host> [^:/]+ )
(?:
:
(?P<port> [0-9]+ )
)?
)
)?
(?:
/
(?P<path> /[^?#]+ )
)?
(?:
\?
(?P<query> [^#]+ )
)?
(?:
#
(?P<fragment> .+ )
)?
$
!ix';
$urls = [
'http://host.com/path',
'http://user@host.com/path',
'http://user:pass@host.com/path',
'http://user:pass@host.com:8080/path',
'//user:pass@host.com:8080/path',
'http://user:pass@host.com:8080/path?query#fragment',
];
foreach ($urls as $str) {
echo "$str\n";
preg_match($expr, $str, $matches);
var_dump($matches);
}
- Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- http://host.com/path
array(19) {
[0]=>
string(20) "http://host.com/path"
["scheme"]=>
string(4) "http"
[1]=>
string(4) "http"
["authority"]=>
string(8) "host.com"
[2]=>
string(8) "host.com"
["user"]=>
string(0) ""
[3]=>
string(0) ""
["pass"]=>
string(0) ""
[4]=>
string(0) ""
["host"]=>
string(8) "host.com"
[5]=>
string(8) "host.com"
["port"]=>
string(0) ""
[6]=>
string(0) ""
["path"]=>
string(0) ""
[7]=>
string(0) ""
["query"]=>
string(0) ""
[8]=>
string(0) ""
["fragment"]=>
string(5) "/path"
[9]=>
string(5) "/path"
}
http://user@host.com/path
array(19) {
[0]=>
string(25) "http://user@host.com/path"
["scheme"]=>
string(4) "http"
[1]=>
string(4) "http"
["authority"]=>
string(13) "user@host.com"
[2]=>
string(13) "user@host.com"
["user"]=>
string(4) "user"
[3]=>
string(4) "user"
["pass"]=>
string(0) ""
[4]=>
string(0) ""
["host"]=>
string(8) "host.com"
[5]=>
string(8) "host.com"
["port"]=>
string(0) ""
[6]=>
string(0) ""
["path"]=>
string(0) ""
[7]=>
string(0) ""
["query"]=>
string(0) ""
[8]=>
string(0) ""
["fragment"]=>
string(5) "/path"
[9]=>
string(5) "/path"
}
http://user:pass@host.com/path
array(19) {
[0]=>
string(30) "http://user:pass@host.com/path"
["scheme"]=>
string(4) "http"
[1]=>
string(4) "http"
["authority"]=>
string(18) "user:pass@host.com"
[2]=>
string(18) "user:pass@host.com"
["user"]=>
string(4) "user"
[3]=>
string(4) "user"
["pass"]=>
string(4) "pass"
[4]=>
string(4) "pass"
["host"]=>
string(8) "host.com"
[5]=>
string(8) "host.com"
["port"]=>
string(0) ""
[6]=>
string(0) ""
["path"]=>
string(0) ""
[7]=>
string(0) ""
["query"]=>
string(0) ""
[8]=>
string(0) ""
["fragment"]=>
string(5) "/path"
[9]=>
string(5) "/path"
}
http://user:pass@host.com:8080/path
array(19) {
[0]=>
string(35) "http://user:pass@host.com:8080/path"
["scheme"]=>
string(4) "http"
[1]=>
string(4) "http"
["authority"]=>
string(23) "user:pass@host.com:8080"
[2]=>
string(23) "user:pass@host.com:8080"
["user"]=>
string(4) "user"
[3]=>
string(4) "user"
["pass"]=>
string(4) "pass"
[4]=>
string(4) "pass"
["host"]=>
string(8) "host.com"
[5]=>
string(8) "host.com"
["port"]=>
string(4) "8080"
[6]=>
string(4) "8080"
["path"]=>
string(0) ""
[7]=>
string(0) ""
["query"]=>
string(0) ""
[8]=>
string(0) ""
["fragment"]=>
string(5) "/path"
[9]=>
string(5) "/path"
}
//user:pass@host.com:8080/path
array(19) {
[0]=>
string(30) "//user:pass@host.com:8080/path"
["scheme"]=>
string(0) ""
[1]=>
string(0) ""
["authority"]=>
string(23) "user:pass@host.com:8080"
[2]=>
string(23) "user:pass@host.com:8080"
["user"]=>
string(4) "user"
[3]=>
string(4) "user"
["pass"]=>
string(4) "pass"
[4]=>
string(4) "pass"
["host"]=>
string(8) "host.com"
[5]=>
string(8) "host.com"
["port"]=>
string(4) "8080"
[6]=>
string(4) "8080"
["path"]=>
string(0) ""
[7]=>
string(0) ""
["query"]=>
string(0) ""
[8]=>
string(0) ""
["fragment"]=>
string(5) "/path"
[9]=>
string(5) "/path"
}
http://user:pass@host.com:8080/path?query#fragment
array(19) {
[0]=>
string(50) "http://user:pass@host.com:8080/path?query#fragment"
["scheme"]=>
string(4) "http"
[1]=>
string(4) "http"
["authority"]=>
string(23) "user:pass@host.com:8080"
[2]=>
string(23) "user:pass@host.com:8080"
["user"]=>
string(4) "user"
[3]=>
string(4) "user"
["pass"]=>
string(4) "pass"
[4]=>
string(4) "pass"
["host"]=>
string(8) "host.com"
[5]=>
string(8) "host.com"
["port"]=>
string(4) "8080"
[6]=>
string(4) "8080"
["path"]=>
string(0) ""
[7]=>
string(0) ""
["query"]=>
string(0) ""
[8]=>
string(0) ""
["fragment"]=>
string(20) "/path?query#fragment"
[9]=>
string(20) "/path?query#fragment"
}
- Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected '[' in /in/k6NSO on line 42
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected '[' in /in/k6NSO on line 42
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/k6NSO on line 42
Process exited with code 255.
preferences:
301.61 ms | 401 KiB | 459 Q