views:

38

answers:

4

I'm using WAMP and the root folder of my page is: http://localhost/projects/bp/

In a Worpress application and I want to give unique id's to the body tag of every Page. So I did the following:

<?php
    $page = $_SERVER['REQUEST_URI'];
    $page = str_replace("/","",$page);
    $page = str_replace(".php","",$page);
    $page = str_replace("?s=","",$page);
    $page = $page ? $page : 'default'
?>

<body id="<?php echo $page ?>">

When I clicked the Aboutpage the URL change to the following: http://localhost/projects/bp/about and $page showed the following value:projectsbpabout

What can I do so that $page just show the last word of the URL. In this case, about, I don't want the projectsbp part)?

Do I have to change something in the Wordpress routing?

+2  A: 

As / is your separator, first create an array of all the loose parts: $parts = explode('/', $_SERVER['REQUEST_URI']); Now you just want the last element: $last_part = end($parts); of course this can also be done in one go:

$last_part = end(explode('/', $_SERVER['REQUEST_URI']));

Dennis Haarbrink
@Dennis Haarbrink sorry I tried that but it didn't work. Should I delete the previous code and replace it by yours?
janoChen
Don’t forget the other higher level separator as `$_SERVER['REQUEST_URI']` contains the requested URI path *and* query (see also Pekka’s answer).
Gumbo
+4  A: 

I would use PHP's built-in path parsing functions to do this.

Use parse_url() to cut off the query and get only the path part:

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

(parse_url is used to parse full URLs but should work fine for this. The second parameter is available since PHP 5.1.2.)

Then use basename() to extract the file name:

$pagename = basename($path, ".php");  // Will remove `.php` suffix

Additional thougths

Depending on how your site is structured, this method will not necessarily make for unique IDs. What if about is a sub-page in /bp and /bc? (If that is possible in Wordpress.) You would have two different pages with the same ID. In that case, you may want to use the full path as an identifier, converting slashes into underlines:

<body id="projects_bp_about">

also from own experience, I recommend using classes for this to avoid ID collisions if a page is named like an already existing elements on the page!

<body class="projects_bp_about">
Pekka
@Pekka Thanks that worked! (Good to always see you around).
janoChen
@Pekka Thanks that worked! (Good to always see you around). One more thing, how can I add a default to $pagename, say home (like, if $pagename is something else, body = home ?
janoChen
@janoChen `$base = basename($path, ".php"); $pagename = ($base != "" ? $base : "home");`
Pekka
@Pekka sorry that didn't work because when the page is in the root folder (http://localhost/projects/bp/) $pagename = bp not sure why.
janoChen
@jano what does `basename()` return in that case? I think it should return `bp`: If it does, replace `$base != ""` by `$base != "bp"`.
Pekka
@Pekka yes is "bp" now works thanks!
janoChen
A: 

to get only the last bit you can use

$page = array_pop(explode('/', $_SERVER['REQUEST_URI']));

explode the string by '/' and get the last piece.

Raja
A: 

instead of using str_replace you can use..

$pageArr = explode("/",$page);

it will give you array with three values you can capture the last one as about

sushil bharwani