Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Titles from Urls...

how to get the last categeory...

         

mipapage

1:13 am on Aug 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello gurus...

Here's the scoop. I have a directory that I've built using a PHP script.

The URLs are much like those form Dmoz:

/cat/sub_cat1/sub_cat2/sub_cat3/sub_cat4/sub_cat5/

I would like to pull out the final sub_cat, strip off the underscore and add this to the title. I've managed to get the whole URL into the title without the slashes, but what I can't do is find a way to get the last category and strip off the underscores.

Any Ideas?

PandaM

3:19 am on Aug 18, 2003 (gmt 0)

10+ Year Member



$full_path = dirname($_SERVER['PHP_SELF']);
$last_cat = substr(strrchr($full_path,'/'),1);
// this should give you the last category name
// then use str_replace to replace all underscore to space
$last_cat_name = str_replace('_',' ',$last_cat);

mipapage

8:29 am on Aug 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PandaM
Thanks a lot!

jatar_k

4:39 pm on Aug 18, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



or

$patharr = preg_split("/\//",$_SERVER['PHP_SELF'],-1,PREG_SPLIT_NO_EMPTY);
echo $patharr[count($patharr)-2];

mipapage

11:50 pm on Aug 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jatar_k,

Thanks to you too. This forum is great. I'm feeling guilty as I am a huge PHP rookie and up to my eyeballs in work. I set up the earlier response and then had to modify things when my url rewrite proved too difficult to implement, so I have gone from getting the last directory to getting the last bit of the $page variable.

I'm going to play around with your example a bit too - I saw similar examples when googling, but truth be told got a little scared by the 'apparent complexity'.

Thanks again!

jatar_k

2:01 am on Aug 19, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



another little function that might come in handy
parse_url [ca.php.net]