我如何得到当前网址的特定部分?例如,我当前的网址是:
http://something.com/index.php?path=/something1/something2/something3/
好吧,我需要用php打印something2
。
谢谢!
答案 0 :(得分:3)
您使用PHP中的explode
函数将URL与第一个参数(在本例中为正斜杠)分开。要实现您的目标,您可以使用;
$url = "http://something.com/index.php?path=/something1/something2/something3/";
$parts = explode('/', $url);
$value = $parts[count($parts) - 2];
答案 1 :(得分:3)
所有这些其他例子似乎都集中在您的确切示例上。我的猜测是你需要一种更灵活的方法来实现这一点,因为如果你的URL发生了变化而你仍然需要从查询字符串中的path
参数中获取数据,那么仅爆炸方法非常脆弱。
我会向您指出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);
答案 2 :(得分:0)
你可以这样做:
$url = explode('/', 'http://something.com/index.php?path=/something1/something2/something3/');
echo $url[5];