1

ユーザーが Web アドレスをテキスト フィールドに入力できるシナリオがあり、そこから 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

(ドメインは google だけでなく、何でもかまいません!)

4

2 に答える 2

1

parse_url()この目的でPHP を使用できますが、これには URI が正しい形式である必要があります。

$parsed = parse_url($url);
var_dump( $parsed['path'] . $parsed['query'] );

URIがで始まる限り、正しい結果が得られますhttp://(そうでない場合、ドメインもパスとして解析されます)。

于 2014-03-12T17:10:27.987 に答える
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";
}
于 2014-03-12T17:21:37.913 に答える