2

現在の URL の特定の部分を取得するにはどうすればよいですか? たとえば、私の現在の URL は次のとおりです。

http://something.com/index.php?path=/something1/something2/something3/

さて、私something2はphpで印刷する必要があります。

ありがとう!

4

3 に答える 3

3

PHPの関数を使用しexplodeて、URL を最初のパラメーター (この場合はスラッシュ) で区切ります。あなたの目標を達成するために使用することができます;

$url = "http://something.com/index.php?path=/something1/something2/something3/";
$parts = explode('/', $url);
$value = $parts[count($parts) - 2];
于 2013-10-08T22:01:17.493 に答える
3

これらの他のすべての例は、正確な例に焦点を当てているようです。pathURL が変更され、クエリ文字列のパラメーターからデータを取得する必要がある場合、爆発のみのアプローチは非常に脆弱であるため、より柔軟な方法が必要であると思います。

と関数について説明しますparse_url()parse_str()

// your URL string
$url = 'http://something.com/index.php?path=/something1/something2/something3/';

// get the query string (which holds your data)
$query_string = parse_url($url, PHP_URL_QUERY);

// load the parameters in the query string into an array
$param_array = array();
parse_str($query_string, $param_array);

// now you can look in the array to deal with whatever parameter you find useful. In this case 'path'

$path = $param_array['path'];

// now $path holds something like '/something1/something2/something3/'
// you can use explode or whatever else you like to get at this value.
$path_parts = explode('/', trim($path, '/'));

// see the value you are interested in
var_dump($path_parts);
于 2013-10-08T22:13:41.693 に答える
0

次のようなことができます。

$url = explode('/', 'http://something.com/index.php?path=/something1/something2/something3/');
echo $url[5];
于 2013-10-08T21:59:25.827 に答える