現在の URL の特定の部分を取得するにはどうすればよいですか? たとえば、私の現在の URL は次のとおりです。
http://something.com/index.php?path=/something1/something2/something3/
さて、私something2
はphpで印刷する必要があります。
ありがとう!
PHPの関数を使用しexplode
て、URL を最初のパラメーター (この場合はスラッシュ) で区切ります。あなたの目標を達成するために使用することができます;
$url = "http://something.com/index.php?path=/something1/something2/something3/";
$parts = explode('/', $url);
$value = $parts[count($parts) - 2];
これらの他のすべての例は、正確な例に焦点を当てているようです。path
URL が変更され、クエリ文字列のパラメーターからデータを取得する必要がある場合、爆発のみのアプローチは非常に脆弱であるため、より柔軟な方法が必要であると思います。
と関数について説明します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);
次のようなことができます。
$url = explode('/', 'http://something.com/index.php?path=/something1/something2/something3/');
echo $url[5];