2015-09-25 52 views
-4

我有这个网址如何在php中解析url?

$url = "http://www.google.pt/this/other/that/file.php"; 

现在我想获得价值

www.google.pt/ /other/that/file.php

$first_after_root = "this"; 
$rest_after_this ="other/that/file.php"; 

我试过用过var_dump(parse_url($url));

+0

['parse_url()'](http://php.net/manual/en/function.parse-url.php)如果您想更详细地处理路径,你应该手动执行此操作。 – NDM

+0

我读过我可以使用类似 $ first_after_root = Explode('/',$ url); –

+0

尝试'var_dump(explode('/',parse_url($ url,PHP_URL_PATH)));' – NDM

回答

1

此解决方案是关于阵列的爆炸/内爆元素。我鼓励你在每一步打印变量以查看中间体步骤。

// Get the path 
$parsed_url = parse_url($url); 
$path = $parsed_url['path']; 

// Explode path and remove empty elements 
$parts = array_filter(explode('/', $path)); 

// Extract the first element of the $parts array 
$first_after_root = array_shift($parts); 
// Assign the other elements 
$rest_after_this = implode('/', $parts); 
+0

我不知道如何得到它的价值,但修复了我 –

+0

非常感谢AlexandreLeprêtre –