我有一个场景,用户可以在文本字段中输入网址,我需要从中返回URI。
因此,用户可能会在以下充实状态下输入地址 -
http://www.google.co.uk/contoller/function?stuff=thing
www.google.co.uk/controller/function?stuff=thing
google.co.uk/controller/function?stuff=thing
/controller/function?stuff=thing
controller/function?stuff=thing
从所有这些例子中我都需要回复:
/controller/function?stuff=thing
(请注意,域名可能是任何内容,而不仅仅是谷歌!)
答案 0 :(得分:1)
您可以将PHP的parse_url()
用于此目的,但这需要URI格式正确。
$parsed = parse_url($url);
var_dump( $parsed['path'] . $parsed['query'] );
只要URI以http://
开头,就会得到正确的结果(否则域也将被解析为路径)。
答案 1 :(得分:1)
$arr = array(
'http://www.google.co.uk/contoller/function?stuff=thing',
'www.google.co.uk/controller/function?stuff=thing',
'google.co.uk/controller/function?stuff=thing',
'/controller/function?stuff=thing',
'controller/function?stuff=thing',
);
foreach($arr as $a){
print "/".preg_replace("@https?://.*?(/|$)|[^\s/]+\.[^\s/]+/|^/@", "", $a) . "\n";
}