Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Creating a Dynamic Breadcrumb: How to call last element in an array?



8:52 pm on Mar 25, 2008 (gmt 0)

5+ Year Member


I have created a nice script that will convert the URL of my page: http//www.example.com/section/subsection/page_name_goes_here to a nice Breadcrumb, like this: Home > Section > Subsection > Page Name Goes Here. I used functions explode, str_replace and ucwords to separate all the strings, remove any spaces or underlines and make all strings uppercase.

All those words are LINKS directing to their respective paths. This works perfect as a practical dynamic breadcrumb and I am pretty happy with the results, but I've run into a problem.
If my page URL is for instance "http://www.example.com/section/index.php", it will output Home > Section > Index. There are two problems here. First, "Index" is supposed to be replaced with the word "Home". And second, in my Dynamic Breadcrumb, I will never want to make the last element in the array a link.

All I need is a function that can call the last element in my array, so I can work with it within my code and make the necessary changes to that last string in my breadcrumb.

Hopefully your help will solve this small bug and then I'll post my function here so others can use it if they find it useful.

Thank you for your help!



9:01 pm on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

$last = [url=http://uk2.php.net/manual/en/function.count.php]count[/url]($array)-1; // as arrays start from 0

As you are already using str_replace, could you not add another to change index.php into home?

Are you sure that you want all index.php to become home?
As using your example that would lead -
to be changed into
Home > Section > Home

[edited by: PHP_Chimp at 9:04 pm (utc) on Mar. 25, 2008]


9:06 pm on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member

To deal with what Chimp is saying
Home > Section > Home

I typically hard core the title of the site as the first "crumb"

So if it is "http://www.example.com/section/index.php"
Example Site > Section > Home


9:26 pm on Mar 25, 2008 (gmt 0)

5+ Year Member

You're both right. And yes, making http//www.example.com/section/subsection/index.php into Example Site > Section > Subsection > Home would be a problem. I don't need home there, right? I can just make the last crumb be "Subsection" and not make that a link.

I'm messing with my code using your code snippet, PHP_Chimp, let me see what I can get.


Featured Threads

Hot Threads This Week

Hot Threads This Month