Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.