homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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

5+ Year Member

Msg#: 3610454 posted 8:52 pm on Mar 25, 2008 (gmt 0)


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!




WebmasterWorld Senior Member 5+ Year Member

Msg#: 3610454 posted 9:01 pm on Mar 25, 2008 (gmt 0)

$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]


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

Msg#: 3610454 posted 9:06 pm on Mar 25, 2008 (gmt 0)

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


5+ Year Member

Msg#: 3610454 posted 9:26 pm on Mar 25, 2008 (gmt 0)

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved