Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a path properly in PHP

Tags:

path

php

split

What is the best way to do the following:

I get a path with an AJAX request

e.g. dir1/dir2/dir3/dir4

I need to present it like this on my webpage:

dir1 >> dir2 >> dir3 >> dir4

each of them being html anchor tags with the href attribute being

/dir1

/dir1/dir2

/dir1/dir2/dir3

/dir1/dir2/dir3/dir4 

respectively

What is the most elegant and efficient way to achieve this?

so far, I'm doing something like this which i think is really dirty:

<?php 
$dirs = explode(PATH_SEPARATOR, $this->metadata["path"]);
    foreach ($dirs as $key=>$val) {
             if ($val == '') {
                 continue;
             }
             $pathArray = array();
             for ($i = 0; $i <= $key; $i++) {
                 array_push($pathArray, $dirs[$i]);
             }
             $path = implode('/', $pathArray);
             echo " >> <a href=" . $path . ">" . truncate($val) . "</a>";
    }
    ?>  
like image 622
nezgerland Avatar asked Jun 09 '11 08:06

nezgerland


People also ask

How to break path in PHP?

<? php $dirs = explode(PATH_SEPARATOR, $this->metadata["path"]); foreach ($dirs as $key=>$val) { if ($val == '') { continue; } $pathArray = array(); for ($i = 0; $i <= $key; $i++) { array_push($pathArray, $dirs[$i]); } $path = implode('/', $pathArray); echo " >> <a href=" .

How do you separate in PHP?

PHP | explode() Function explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs.

Which of the following function is used to split file path in PHP?

So on further reflection, I started my solution using basename() . This returns the file name and extension for a given file path. From here, I used explode to split the base file name into it's file name and extension.

What is split function in PHP?

PHP - Function split() The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.


2 Answers

Something like this maybe (if I got your intention right):

<?php
$str = 'dir1/dir2/dir3/dir4';

$output = array();
$chunks = explode('/', $str);
foreach ($chunks as $i => $chunk) {
    $output[] = sprintf(
        '<a href="#/%s">%s</a>',
        implode('/', array_slice($chunks, 0, $i + 1)),
        $chunk
    );
}

echo implode(' &gt;&gt; ', $output);

Output:

<a href="#/dir1">dir1</a> &gt;&gt; 
<a href="#/dir1/dir2">dir2</a> &gt;&gt;
<a href="#/dir1/dir2/dir3">dir3</a> &gt;&gt;
<a href="#/dir1/dir2/dir3/dir4">dir4</a>
like image 153
Yoshi Avatar answered Sep 21 '22 16:09

Yoshi


Var 1

<?php

    $str = '/dir1/dir2/dir3/dir4';
    $arr = array_filter(explode('/',$str));
    $out = array('/'.implode('/',$arr).'/');
    while((array_pop($arr) and !empty($arr))){
        $out[] = '/'.implode('/',$arr).'/';
    };

    print_r($out);

    /*
    Array(
       [0] => /dir1/dir2/dir3/dir4/
       [1] => /dir1/dir2/dir3/
       [2] => /dir1/dir2/
       [3] => /dir1/
    )
    */

?>

Var 2 ( Links )

<?php

    $str = '/dir1/dir2/dir3/dir4';
    $arr = array_filter(explode('/',$str));
    $out = array('<a href="/'.implode('/',$arr).'/">'.basename($str).'</a>');
    while((array_pop($arr) and !empty($arr))){
        $out[] = '<a href="/'.implode('/',$arr).'/">'.end($arr).'</a>';
    };

    print_r($out);

    /*
    Array
    (
        [0] => <a href="/dir1/dir2/dir3/dir4/">dir4</a>
        [1] => <a href="/dir1/dir2/dir3/">dir3</a>
        [2] => <a href="/dir1/dir2/">dir2</a>
        [3] => <a href="/dir1/">dir1</a>
    )
    */

?>
like image 38
user3126867 Avatar answered Sep 23 '22 16:09

user3126867