How to split a path properly in PHP

前端 未结 4 672
别那么骄傲
别那么骄傲 2021-02-13 14:52

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 we

相关标签:
4条回答
  • 2021-02-13 15:23

    if all you need to do is explode by forward slash there is a simple solution that works everytime.

    check this out

    $path = 'dir1/dir2/dir3/dir4';
    
    $array = explode('/', ltrim($path, '/'));
    
    print_r($array);
    

    enjoy :D

    0 讨论(0)
  • 2021-02-13 15:28

    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>
    
    0 讨论(0)
  • 2021-02-13 15:28

    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>
        )
        */
    
    ?>
    
    0 讨论(0)
  • 2021-02-13 15:46

    I'd do it like this...

    <?php
    
    $path = 'dir1/dir2/dir3/dir4';
    $dirs = explode('/', $path);
    
    while (count($dirs) > 0) {
        $link = '/' . implode($dirs, '/');
        $text = array_pop($dirs);
        $breadcrumb = "<a href=\"$link\">$text</a>" . $breadcrumb;
        if (count($dirs) > 0) {
            $breadcrumb = ' &gt;&gt; ' . $breadcrumb;
        }
    }
    
    echo $breadcrumb;
    

    If you are getting this path based on the URL you shouldn't need to use DIRECTORY_SEPARATOR because URLs should always use /. If it is derived from the filesystem path you would need to swap it in instead of '/' on line 4.

    0 讨论(0)
提交回复
热议问题