Mutley Posted December 4, 2009 Share Posted December 4, 2009 What's a good way to do this? Need some ideas so I don't make an unnecessarily long script. Simply want to convert the URL of a website into breadcrumbs I can echo in the design, all the page URLs are in the same format. mydomain.com/services/computer-repair to Home > Services > Computer Repair There's a few things... 1) Breaking the URL down to sections, not sure what to do here 2) Make first letters Capitals (ucfirst?) 3) Convert Hyphens to Spaces (preg_replace?) Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/183968-breadcrumbs-from-url/ Share on other sites More sharing options...
cags Posted December 4, 2009 Share Posted December 4, 2009 Dozens of way todo it... $url = "mydomain.com/services/computer-repair"; $parts = explode("/", $url); $parts[0] = "Home"; $output = implode(" > ", $parts); $output = ucwords(str_replace("-", " ", $output)); Link to comment https://forums.phpfreaks.com/topic/183968-breadcrumbs-from-url/#findComment-971143 Share on other sites More sharing options...
Mutley Posted December 4, 2009 Author Share Posted December 4, 2009 That was a lot simpler to how I was trying to do it! Thanks alot! Couple of questions, if the URL has a trailing slash, how do I remove the unwanted '>' at the end? And anyway to make each part a URL to that part of the URL... <a href="domain.com">Home</a> > <a href="/services/">Services</a> > <a href="/computer-repair/">Computer Repair</a> Link to comment https://forums.phpfreaks.com/topic/183968-breadcrumbs-from-url/#findComment-971156 Share on other sites More sharing options...
cags Posted December 4, 2009 Share Posted December 4, 2009 To get rid of the trailing >, simply change the first explode to... $parts = explode("/", rtrim($url, "/")); As for the second part, before I try and work it out does the third href really want to be /computer-repair/ and not /services/computer-repair/? Link to comment https://forums.phpfreaks.com/topic/183968-breadcrumbs-from-url/#findComment-971160 Share on other sites More sharing options...
Mutley Posted December 4, 2009 Author Share Posted December 4, 2009 To get rid of the trailing >, simply change the first explode to... $parts = explode("/", rtrim($url, "/")); As for the second part, before I try and work it out does the third href really want to be /computer-repair/ and not /services/computer-repair/? Yes, sorry you are correct, /servicces/computer-repair/ Link to comment https://forums.phpfreaks.com/topic/183968-breadcrumbs-from-url/#findComment-971162 Share on other sites More sharing options...
cags Posted December 4, 2009 Share Posted December 4, 2009 Try... $url = "mydomain.com/services/computer-repair/"; $parts = explode("/", rtrim($url, "/")); $links = $parts; $links[0] = '<a href="/">Home</a>'; foreach($links as $k=>$v) { $link = '/'; for($i = 1; $i <= $k; $i++) { $link .= $parts[$i] . "/"; } $links[$k] = '<a href="' . $link . '">' . ucwords(str_replace("-", " ", $v)) . '</a>'; } echo $output = implode(" > ", $links); Link to comment https://forums.phpfreaks.com/topic/183968-breadcrumbs-from-url/#findComment-971172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.