开发者

Split URL in to page and folder variables using php?

I am currently using the following code to achieve a page, section and class variable from the 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;
}

For example if the url is http://www.mydomain.co.uk/about/ the code will return the following variables:

$page = "about"
$section = "about"
$class = "abou开发者_运维技巧t"

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

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

But when I add more depth for example For http://www.mydomain.co.uk/about/general-info/history/ to code produces:

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

where ideally I need it to output the following:

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

or breakdown the sections into as many as needed for example:

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

Hopefully someone can help. If anything is unclear please ask.


What about using more general splitting ?

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

Then do your routing as you want:

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


So you want an URI-scheme as follows?

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

Special cases:

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

Instead of relying on basename(), you should split your string into an array using explode() or preg_split(). You can use REQUEST_URI directly since the domain name does not give any extra information for your sections and page.

Once you have an array, you can easily count() the number of path components, handle special cases for empty and size-one paths, and so on. In the following example, I use array_pop() to extract the last part of the path to separate the page from the sections. Since you seem to desire space-separated strings for sections and page, I use implode() to join the arrays back into a string.

// 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜