2011-05-26 38 views
1

我目前使用下面的代码来实现网址中的页面,节和类变量。使用php将URL分割为页面和文件夹变量?

$domain = 'http://' . $_SERVER['HTTP_HOST']; 
$path = $_SERVER['REQUEST_URI']; 
$url = $domain . $path; 

// page + section + class 
$page = basename($url); 
$page = $class = str_replace('.php','',$page); 
$page = str_replace('-',' ',$page); 

if ($path == "/") { 
    $section = $class = "home"; 
} else if (basename(dirname($url),"/") == $_SERVER['HTTP_HOST']) { 
    $section = $page; 
} else { 
    $section = basename(dirname($url),"/"); 
    $section = str_replace('-',' ',$section); 
    $class = basename(dirname($url),"/") . " " . $class; 
} 

例如,如果URL是http://www.mydomain.co.uk/about/代码将返回以下变量:

$page = "about" 
$section = "about" 
$class = "about" 

对于http://www.mydomain.co.uk/about/general-info/

$page = "general info" 
$section = "about" 
$class = "about general-info" 

但是当我添加更多的深度例如为http://www.mydomain.co.uk/about/general-info/history/代码生产:

$page = "history" 
$section = "general info" 
$class = "general-info history" 

理想的地方我需要它输出如下:

$page = "history" 
$section = "about general info" 
$class = "about general-info history" 

或击穿节放到尽可能多的需要,例如:

$section1 = "about" 
$section2 = "general-info" 

希望有人可以提供帮助。如果有什么不清楚的地方,请询问。

+0

请问可以告诉我,直到网址会有多少层次?像这里你已经提供了直到第三级。 – 2011-05-26 13:37:11

+0

在大多数情况下,它只会持续到第3天。它可能会去第四次,但目前第三 – 2011-05-26 13:39:09

+0

检查费迪南德的第一个答案。它也是我喜欢的。 – 2011-05-26 13:41:10

回答

2

所以,你想要一个URI的方案如下?

http://<domain>/<section-1>/<section-2>/.../<page>/ 

特殊情况:

(1) http://<domain>/   -- page is empty, section is "home" 
(2) http://<domain>/<section>/ -- page and section are '<section>' 

不是依靠basename(),你应该将字符串分割成使用explode()preg_split()数组。您可以直接使用REQUEST_URI,因为域名不会为您的部分和页面提供任何额外信息。

一旦你有一个数组,你可以很容易地得到count()路径组件的数量,处理空和一号路径的特殊情况,等等。在下面的示例中,我使用array_pop()来提取路径的最后部分,以将页面与部分分开。由于您似乎希望分区和页面使用空格分隔的字符串,因此我使用implode()将数组连接回字符串。

// No need for the domain stuff! 
$path = $_SERVER['REQUEST_URI']; 

// Split at '/', could use explode() but the PREG_SPLIT_NO_EMPTY flag is 
// very handy since it handles "//" and "/" at start/end. 
$tokens = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY); 

if (count($tokens) == 0) { 
    // Special case 1 
    $page = ""; 
    $section = $class = 'home'; 
} elseif (count($tokens) == 1) { 
    // Specical case 2 
    $page = $section = $class = $tokens[0]; 
} else { 
    // Class contains all tokens. 
    $class = implode(' ', $tokens); 

    // The last part is the page. 
    $page = array_pop($tokens); 

    // Everything else are sections. 
    $sections = implode(' ', $tokens); 
} 

// You seem to want spaces for dashes in the section: 
$section = str_replace('-', ' ', $section); 
+0

我会给代码一个现在去。先谢谢你! – 2011-05-26 13:52:33

+0

很好,非常感谢。通过在$ page和$部分进行测试,可以灵活地显示和隐藏整个站点中的某些元素。这是一个基于php的网站,使用perch作为cms,因此不会控制页面。 $ class只是将一个简单的类添加到body标签中,仅仅是将页面需要的样式略微不同。 要测试页面或节中,我一直在使用下列内容: '' 它的工作原理,但这种正确或最好的方式去? – 2011-05-26 15:04:11

+0

要测试某个节,您应该跳过'implode()'部分,将节列表保存为一个数组,并使用'in_array(“general-information”,$ sections)'来代替。请参阅:http://www.php.net/in_array – 2011-05-26 15:55:24

4

如何使用更一般的分裂?

// Url: http://www.mydomain.co.uk/about/general-info/history/ 
$slices = explode('/', $_SERVER['REQUEST_URI']); 
// $slices == ['about', 'general-info', 'history'] 

然后做你的路由,只要你想:

$class = implode(' ', $slices); 
$section = $slices[1]; 
// etc. 
+0

简单是更好的伊莫。 – torinagrippa 2015-06-09 15:38:25