Click here to Skip to main content
15,916,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need a little help here i have a breadcrumbs in php, my directory is like this

folder/name.php


and using this code i get on output like this

Home » folder » name


what i want to do is to put name that i want instead of the code gets the name of folder and file. please help me in this. thanks guys

my cod is below:

What I have tried:

<?php 
    $path = $_SERVER["PHP_SELF"];
    $parts = explode('/',$path);
    if (count($parts) < 2)
    {
    echo("home");
    }
    else
    {
    echo ("<a href=\"/\">Home</a> » ");
    for ($i = 1; $i < count($parts); $i++)
        {
        if (!strstr($parts[$i],"."))
            {
            echo("<a href=\"");
            for ($j = 0; $j <= $i; $j++) {echo $parts[$j]."/";};
            echo("\">". str_replace('-', ' ', $parts[$i])."</a> » ");
            }
        else
            {
            $str = $parts[$i];
            $pos = strrpos($str,".");
            $parts[$i] = substr($str, 0, $pos);
            echo str_replace('-', ' ', $parts[$i]);
            };
        };
    };  
?>
Posted
Updated 13-Feb-17 16:59pm
v2

1 solution

There is no magic. First, prepare a list of dictionary that match all possible folder or file names to their desirable new names, in PHP, use associative array, e.g.
// an associative array of oldnames => newnames
$newnames = array("oldname1"=>"newname1", "oldname2"=>"newname2")

then, in the code of your, replace the appropriate
echo $parts[$j]
, depending on whether it is for descriptive purpose for for actual href link, with
echo $newnames[$parts[$j]]
You will have to figure it out based on your requirement. Of course, make sure the $parts values are included as keys in the $newnames.
Reference: PHP 5 Arrays[^]
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900