How to split a path properly in PHP

前端 未结 4 677
别那么骄傲
别那么骄傲 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:46

    I'd do it like this...

     0) {
        $link = '/' . implode($dirs, '/');
        $text = array_pop($dirs);
        $breadcrumb = "$text" . $breadcrumb;
        if (count($dirs) > 0) {
            $breadcrumb = ' >> ' . $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.

提交回复
热议问题