Jump to content

Breadcrumbs from URL?


Mutley

Recommended Posts

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

That was a lot simpler to how I was trying to do it! Thanks alot!  :D

 

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.