sdfsdfsdf
sdfsdfsdf

Reputation: 23

How can I convert a file path into different parts?

I want to create a small code that echo a filepath with anchor elements for every singe path directory.

So all I want is to explode a path to the single values. example:

$file_dir = "1/2/3/4";

the result:

<a href="1">1</a>/
<a href="1/2">2</a>/
<a href="1/2/3">3</a>/
<a href="1/2/3/4">4</a>/

In the browser: 1/2/3/4 (every number is click able and will link the user to that directory)

My current code:

$file_dir = "1/2/3/4";
$output = array();
$devisions = explode('/', $file_dir);
foreach ($chunks as $i => $devisions) {
    $output[] = '<a href="'.$devisions.'">'.$chunk.'</a>';
}

print_r($output);

The result is not so nice. I get so many errors that I don't know where to start.

Can anybody help me?

Upvotes: 2

Views: 50

Answers (3)

Rizier123
Rizier123

Reputation: 59701

This should work for you:

First explode() the string into an array, which will look something like this:

Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Then count() the array to get the amount of parts which you have.

After this you can simply use a for loop where you can always slice a bigger array part from the array with array_slice() and implode() it again into a string.

Means in the loop this happens:

              |                   array_slice()                 |        implode()
------------------------------------------------------------------------------------------
1. iteration: | Array ( [0] => 1 )                              |          1
2. iteration: | Array ( [0] => 1 [1] => 2 )                     |          1/2
3. iteration: | Array ( [0] => 1 [1] => 2 [2] => 3 )            |          1/2/3
4. iteration: | Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )   |          1/2/3/4

Code:

<?php

    $file_dir = "1/2/3/4";
    $parts = explode("/", $file_dir);
    $amount = count($parts);

    for($i = 1; $i <= $amount; $i++)
        echo "<a href='" . implode("/", array_slice($parts, 0, $i)) . "'>" . $parts[$i-1] . "</a>";

?>

output:

<a href='1'>1</a>
<a href='1/2'>2</a>
<a href='1/2/3'>3</a>
<a href='1/2/3/4'>4</a>

Upvotes: 3

Funk Doc
Funk Doc

Reputation: 1643

This works

<?php
$file_dir = "1/2/3/4";
$ex=explode('/',$file_dir);
foreach($ex as $x){
$href[]=$x;
$output[]="<a href='".implode('/',$href)."'>$x</a>";
}
echo implode('<br />',$output);
?>

Upvotes: 0

Felk
Felk

Reputation: 8224

strpos, strrpos and substr seem very suitable for this job.

"Climb" through the occurences of slashes to find the substrings. Use strrpos to get the bit after the last slash of that substring

$file_dir = "1/2/3/4";
$index = 0;
while (($index = strpos($file_dir, "/", $index)) !== false) {
    $file = substr($file_dir, 0, $index);
    $last = substr($file, strrpos($file, "/"));
    echo "<a href='$file'>$last</a>/";
}

EDIT: My solution misses the rightmost element, after the last slash. One way to fix this is to manually echo it after the loop:

$file = substr($file_dir, $index);
echo "<a href='$file'>$file</a>/";

Upvotes: 0

Related Questions