【问题标题】:How to join filesystem path strings in PHP?如何在 PHP 中加入文件系统路径字符串?
【发布时间】:2010-11-08 15:10:54
【问题描述】:

PHP 中是否有内置函数可以智能地连接路径字符串?给定abc/de//fg/x.php 作为参数的函数应该返回abc/de/fg/x.php;应该使用abc/defg/x.php 作为该函数的参数给出相同的结果。

如果没有,是否有可用的课程?它对于拆分路径或删除部分路径也可能很有价值。如果你写了一些东西,可以在这里分享你的代码吗?

可以一直使用/,我只为 Linux 编写代码。

在 Python 中有 os.path.join,这很棒。

【问题讨论】:

  • 哇,我刚从 Google 来到这里。不敢相信PHP是这样的。
  • 请注意,您的示例非常具有误导性,因为os.path.join('some/relative/path, '/an/absolute/path') 将始终返回/an/absolute/path。因此,您要么在寻找 os.path.join 替代品(然后修复您的示例),要么寻找与之接近的东西,但第二个(或第 n 个)的绝对路径被视为相对路径。
  • @Tibo,以及他评论的支持者:不,这个例子根本不会误导,除非有人忽略了问题的要点并坚持过分强调 Python 参考,这是一个边缘方向备注。这个问题非常清楚它想要什么,这完全是os.path.join。阅读:“给定 abc/de//fg/x.php 作为参数的函数应该返回 abc/de/fg/x.php”。
  • PHP 没有内置函数,这很恶心

标签: php string file


【解决方案1】:

作为一个有趣的项目,我创建了另一个解决方案。应该对所有操作系统通用。

对于 PHP 7.2+:

<?php

/**
 * Join string into a single URL string.
 *
 * @param string $parts,... The parts of the URL to join.
 * @return string The URL string.
 */
function join_paths(...$parts) {
    if (sizeof($parts) === 0) return '';
    $prefix = ($parts[0] === DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '';
    $processed = array_filter(array_map(function ($part) {
        return rtrim($part, DIRECTORY_SEPARATOR);
    }, $parts), function ($part) {
        return !empty($part);
    });
    return $prefix . implode(DIRECTORY_SEPARATOR, $processed);
}

7.2之前的PHP版本:

/**
 * Join string into a single URL string.
 *
 * @param string $parts,... The parts of the URL to join.
 * @return string The URL string.
 */
function join_paths() {
    $parts = func_get_args();
    if (sizeof($parts) === 0) return '';
    $prefix = ($parts[0] === DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '';
    $processed = array_filter(array_map(function ($part) {
        return rtrim($part, DIRECTORY_SEPARATOR);
    }, $parts), function ($part) {
        return !empty($part);
    });
    return $prefix . implode(DIRECTORY_SEPARATOR, $processed);
}

对其行为的一些测试用例。

// relative paths
var_dump(join_paths('hello/', 'world'));
var_dump(join_paths('hello', 'world'));
var_dump(join_paths('hello', '', 'world'));
var_dump(join_paths('', 'hello/world'));
echo "\n";

// absolute paths
var_dump(join_paths('/hello/', 'world'));
var_dump(join_paths('/hello', 'world'));
var_dump(join_paths('/hello/', '', 'world'));
var_dump(join_paths('/hello', '', 'world'));
var_dump(join_paths('', '/hello/world'));
var_dump(join_paths('/', 'hello/world'));

结果:

string(11) "hello/world"
string(11) "hello/world"
string(11) "hello/world"
string(11) "hello/world"

string(12) "/hello/world"
string(12) "/hello/world"
string(12) "/hello/world"
string(12) "/hello/world"
string(12) "/hello/world"
string(12) "/hello/world"

更新:增加了7.2之前支持PHP的版本。

【讨论】:

  • +1 非常感谢@Koala Yeung。您的解决方案与 v7.2 以上的 PHP 版本兼容。接受的答案中的代码使用了 create_function() ,它在 7.2 以上的版本中已被弃用 - 虽然在 2013 年我们刚刚拥有 PHP v5.5。
  • 感谢 Koala Yeung!这很好地改进了@Riccardo Galli 非常有用的解决方案,这是我之前的任务。我找不到解决这个问题的方法,只要你的 PHP 是相当最新的,它就可以移植。我对此很满意。
  • @JackLeEmmerdeur:添加了一个适用于旧 PHP 的版本。但是,IMO,PHP 7.2+ 更易于阅读。
【解决方案2】:

适用于需要执行 Windows 反斜杠和 Linux 正斜杠的连接功能的人。

用法:

<?php
use App\Util\Paths
echo Paths::join('a','b'); //Prints 'a/b' on *nix, or 'a\\b' on Windows

类文件:

<?php
namespace App\Util;

class Paths
{
  public static function join_with_separator($separator, $paths) {
    $slash_delimited_path = preg_replace('#\\\\#','/', join('/', $paths));
    $duplicates_cleaned_path = preg_replace('#/+#', $separator, $slash_delimited_path);
    return $duplicates_cleaned_path;
  }

  public static function join() {
    $paths = array();

    foreach (func_get_args() as $arg) {
      if ($arg !== '') { $paths[] = $arg; }
    }
    return Paths::join_with_separator(DIRECTORY_SEPARATOR, $paths);
  }
}

这里是测试函数:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use App\Util\Paths;

class PathsTest extends TestCase
{
  public function testWindowsPaths()
  {
    $TEST_INPUTS = [
      [],
      ['a'],
      ['a','b'],
      ['C:\\','blah.txt'],
      ['C:\\subdir','blah.txt'],
      ['C:\\subdir\\','blah.txt'],
      ['C:\\subdir','nested','1/2','blah.txt'],
    ];
    $EXPECTED_OUTPUTS = [
      '',
      'a',
      'a\\b',
      'C:\\blah.txt',
      'C:\\subdir\\blah.txt',
      'C:\\subdir\\blah.txt',
      'C:\\subdir\\nested\\1\\2\\blah.txt',
    ];
    for ($i = 0; $i < count($TEST_INPUTS); $i++) {
      $actualPath = Paths::join_with_separator('\\', $TEST_INPUTS[$i]);
      $expectedPath = $EXPECTED_OUTPUTS[$i];
      $this->assertEquals($expectedPath, $actualPath);
    }
  }
  public function testNixPaths()
  {
    $TEST_INPUTS = [
      [],
      ['a'],
      ['a','b'],
      ['/home','blah.txt'],
      ['/home/username','blah.txt'],
      ['/home/username/','blah.txt'],
      ['/home/subdir','nested','1\\2','blah.txt'],
    ];
    $EXPECTED_OUTPUTS = [
      '',
      'a',
      'a/b',
      '/home/blah.txt',
      '/home/username/blah.txt',
      '/home/username/blah.txt',
      '/home/subdir/nested/1/2/blah.txt',
    ];
    for ($i = 0; $i < count($TEST_INPUTS); $i++) {
      $actualPath = Paths::join_with_separator('/', $TEST_INPUTS[$i]);
      $expectedPath = $EXPECTED_OUTPUTS[$i];
      $this->assertEquals($expectedPath, $actualPath);
    }
  }
}

【讨论】:

    【解决方案3】:

    嗯,大多数似乎有点过于复杂。不知道,这是我的看法:

    // Takes any amount of arguments, joins them, then replaces double slashes
    function join_urls() {
       $parts = func_get_args();
       $url_part = implode("/", $parts);
       return preg_replace('/\/{1,}/', '/', $url_part);
    }
    

    【讨论】:

      【解决方案4】:

      这是我的解决方案:

      function joinPath(): string {
      
              $path = '';
              foreach (func_get_args() as $numArg => $arg) {
      
                  $arg = trim($arg);
      
                  $firstChar = substr($arg, 0, 1);
                  $lastChar = substr($arg, -1);
      
                  if ($numArg != 0 && $firstChar != '/') {
                      $arg = '/'.$arg;
                      }
      
                  # Eliminamos el slash del final
                  if ($lastChar == '/') {
                      $arg = rtrim($arg, '/');
                      }
      
                  $path .= $arg;
                  }
      
              return $path;
              }
      

      【讨论】:

        【解决方案5】:

        优雅的受 Python 启发的 PHP 单行方式加入路径。

        这段代码没有使用不必要的数组。

        多平台

        function os_path_join(...$parts) {
          return preg_replace('#'.DIRECTORY_SEPARATOR.'+#', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, array_filter($parts)));
        }
        

        基于 Unix 的系统

        function os_path_join(...$parts) {
          return preg_replace('#/+#', '/', implode('/', array_filter($parts)));
        }
        

        没有 REST 参数的基于 Unix 的系统(不尊重明确的 PEP8 理念):

        function os_path_join() {
          return preg_replace('#/+#', '/', implode('/', array_filter(func_get_args())));
        }
        

        用法

        $path = os_path_join("", "/", "mydir/", "/here/");
        

        奖励:如果你真的想遵循 Python os.path.join()。第一个参数是必需的:

        function os_path_join($path=null, ...$paths) {
          if (!is_null($path)) {
            throw new Exception("TypeError: join() missing 1 required positional argument: 'path'", 1);
          }
          $path = rtrim($path, DIRECTORY_SEPARATOR);
          foreach ($paths as $key => $current_path) {
            $paths[$key] = $paths[$key] = trim($current_path, DIRECTORY_SEPARATOR);
          }
          return implode(DIRECTORY_SEPARATOR, array_merge([$path], array_filter($paths)));
        }
        

        如果需要,请检查 os.path.join() 源:https://github.com/python/cpython/blob/master/Lib/ntpath.py

        警告:此解决方案不适用于 url。

        【讨论】:

          【解决方案6】:
          function path_combine($paths) {
            for ($i = 0; $i < count($paths); ++$i) {
              $paths[$i] = trim($paths[$i]);
            }
          
            $dirty_paths = explode(DIRECTORY_SEPARATOR, join(DIRECTORY_SEPARATOR, $paths));
            for ($i = 0; $i < count($dirty_paths); ++$i) {
              $dirty_paths[$i] = trim($dirty_paths[$i]);
            }
          
            $unslashed_paths = array();
          
            for ($i = 0; $i < count($dirty_paths); ++$i) {
              $path = $dirty_paths[$i];
              if (strlen($path) == 0) continue;
              array_push($unslashed_paths, $path);
            }
          
            $first_not_empty_index = 0;
            while(strlen($paths[$first_not_empty_index]) == 0) {
              ++$first_not_empty_index;
            }
            $starts_with_slash = $paths[$first_not_empty_index][0] == DIRECTORY_SEPARATOR;
          
            return $starts_with_slash
              ? DIRECTORY_SEPARATOR . join(DIRECTORY_SEPARATOR, $unslashed_paths)
              : join(DIRECTORY_SEPARATOR, $unslashed_paths);
          }
          

          示例用法:

          $test = path_combine([' ', '/cosecheamo', 'pizze', '///// 4formaggi', 'GORGONZOLA']);
          echo $test;
          

          将输出:

          /cosecheamo/pizze/4formaggi/GORGONZOLA
          

          【讨论】:

            【解决方案7】:

            基于the answer by mpen 的独立于操作系统的版本,但被封装在一个函数中,并且可以选择添加尾随路径分隔符。

            function joinPathParts($parts, $trailingSeparator = false){
                return implode(
                    DIRECTORY_SEPARATOR, 
                    array_map(
                        function($s){
                            return rtrim($s,DIRECTORY_SEPARATOR);
                        }, 
                        $parts)
                    )
                    .($trailingSeparator ? DIRECTORY_SEPARATOR : '');
            }
            

            或者给你单线爱好者:

            function joinPathParts($parts, $trailingSeparator = false){
                return implode(DIRECTORY_SEPARATOR, array_map(function($s){return rtrim($s,DIRECTORY_SEPARATOR);}, $parts)).($trailingSeparator ? DIRECTORY_SEPARATOR : '');
            }
            

            只需使用路径部分数组调用它:

            // No trailing separator - ex. C:\www\logs\myscript.txt
            $logFile = joinPathParts([getcwd(), 'logs', 'myscript.txt']);
            
            // Trailing separator - ex. C:\www\download\images\user1234\
            $dir = joinPathParts([getcwd(), 'download', 'images', 'user1234'], true);
            

            【讨论】:

              【解决方案8】:

              我喜欢Riccardo's answer,我认为这是最好的答案。

              我正在使用它来加入 url 构建中的路径,但有一个小改动来处理协议的双斜杠:

              function joinPath () {
                  $paths = array();
              
                  foreach (func_get_args() as $arg) {
                      if ($arg !== '') { $paths[] = $arg; }
                  }
              
                  // Replace the slash with DIRECTORY_SEPARATOR
                  $paths = preg_replace('#/+#', '/', join('/', $paths));
                  return preg_replace('#:/#', '://', $paths);
              }
              

              【讨论】:

                【解决方案9】:

                如果你知道文件/目录存在,你可以添加额外的斜杠(这可能是不必要的),然后调用realpath,即

                realpath(join('/', $parts));

                这当然和 Python 版本不太一样,但在很多情况下可能已经足够好了。

                【讨论】:

                  【解决方案10】:

                  下面的解决方案使用@RiccardoGalli 提出的逻辑,但经过改进以利用@Qix 和@FélixSaparelli 建议的DIRECTORY_SEPARATOR 常量,更重要的是,修剪每个给定元素 以避免在最终路径中出现仅包含空格的文件夹名称(在我的情况下这是一个要求)。

                  关于preg_replace() 模式中目录分隔符的转义,如您所见,我使用了preg_quote() 函数,它可以很好地完成工作。
                  此外,我只会替换 多个分隔符(RegExp 量词 {2,})。

                  // PHP 7.+
                  function paths_join(string ...$parts): string {
                      $parts = array_map('trim', $parts);
                      $path = [];
                  
                      foreach ($parts as $part) {
                          if ($part !== '') {
                              $path[] = $part;
                          }
                      }
                  
                      $path = implode(DIRECTORY_SEPARATOR, $path);
                  
                      return preg_replace(
                          '#' . preg_quote(DIRECTORY_SEPARATOR) . '{2,}#',
                          DIRECTORY_SEPARATOR,
                          $path
                      );
                  }

                  【讨论】:

                    【解决方案11】:

                    我的看法:

                    function trimds($s) {
                        return rtrim($s,DIRECTORY_SEPARATOR);
                    }
                    
                    function joinpaths() {
                        return implode(DIRECTORY_SEPARATOR, array_map('trimds', func_get_args()));
                    }
                    

                    我会为 trimds 使用匿名函数,但旧版本的 PHP 不支持它。

                    例子:

                    join_paths('a','\\b','/c','d/','/e/','f.jpg'); // a\b\c\d\e\f.jpg (on Windows)
                    

                    更新 2013 年 4 月 2014 年 3 月 2018 年 5 月

                    function join_paths(...$paths) {
                        return preg_replace('~[/\\\\]+~', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $paths));
                    }
                    

                    这将更正任何斜杠以匹配您的操作系统,不会删除前导斜杠,并连续清理多个斜杠。

                    【讨论】:

                    • 它总是创建一个绝对路径,但至少有人提到 DIRECTORY_SEPARATOR...
                    【解决方案12】:

                    找到的最佳解决方案:

                    function joinPaths($leftHandSide, $rightHandSide) { 
                        return rtrim($leftHandSide, '/') .'/'. ltrim($rightHandSide, '/'); 
                    }
                    

                    注意:从 user89021 的评论中复制

                    【讨论】:

                      【解决方案13】:

                      来自 Ricardo Galli 的精彩回答,为避免杀死协议前缀进行了一些改进。

                      这个想法是在一个参数中测试协议的存在,并将其保留到结果中。警告:这是一个幼稚的实现!

                      例如:

                      array("http://domain.de","/a","/b/")
                      

                      结果(保持协议)

                      "http://domain.de/a/b/"
                      

                      而不是(杀死协议)

                      "http:/domain.de/a/b/"
                      

                      但是http://codepad.org/hzpWmpzk需要更好的代码编写技巧。

                      【讨论】:

                        【解决方案14】:

                        这似乎工作得很好,对我来说看起来相当整洁。

                        private function JoinPaths() {
                          $slash = DIRECTORY_SEPARATOR;
                          $sections = preg_split(
                                  "@[/\\\\]@",
                                  implode('/', func_get_args()),
                                  null,
                                  PREG_SPLIT_NO_EMPTY);
                          return implode($slash, $sections);
                        }
                        

                        【讨论】:

                          【解决方案15】:

                          我喜欢提出的几种解决方案。但是那些将所有 '/+' 替换为 '/' (正则表达式)的人忘记了 python 中的 os.path.join() 可以处理这种连接:

                          os.path.join('http://example.com/parent/path', 'subdir/file.html')
                          

                          结果:'http://example.com/parent/path/subdir/file.html'

                          【讨论】:

                          • PHP 的问题,Python 的答案
                          【解决方案16】:

                          这是一个行为类似于Node's path.resolve的函数:

                          function resolve_path() {
                              $working_dir = getcwd();
                              foreach(func_get_args() as $p) {
                                  if($p === null || $p === '') continue;
                                  elseif($p[0] === '/') $working_dir = $p;
                                  else $working_dir .= "/$p";
                              }
                              $working_dir = preg_replace('~/{2,}~','/', $working_dir);
                              if($working_dir === '/') return '/';
                              $out = [];
                              foreach(explode('/',rtrim($working_dir,'/')) as $p) {
                                  if($p === '.') continue;
                                  if($p === '..') array_pop($out);
                                  else $out[] = $p;
                              }
                              return implode('/',$out);
                          }
                          

                          测试用例:

                          resolve_path('/foo/bar','./baz')         # /foo/bar/baz
                          resolve_path('/foo/bar','/tmp/file/')    # /tmp/file
                          resolve_path('/foo/bar','/tmp','file')   # /tmp/file
                          resolve_path('/foo//bar/../baz')         # /foo/baz
                          resolve_path('/','foo')                  # /foo
                          resolve_path('/','foo','/')              # /
                          resolve_path('wwwroot', 'static_files/png/', '../gif/image.gif') 
                                                            # __DIR__.'/wwwroot/static_files/gif/image.gif'
                          

                          【讨论】:

                            【解决方案17】:

                            由于这似乎是一个流行的问题,并且 cmets 充满了“功能建议”或“错误报告”......所有这些代码 sn-p 所做的就是用斜线连接两个字符串,而不会在它们之间重复斜线。就这样。不多也不少。它不会评估硬盘上的实际路径,也不会实际保留起始斜线(如果需要,将其添加回来,至少您可以确保此代码始终返回一个字符串 without 起始斜线)。

                            join('/', array(trim("abc/de/", '/'), trim("/fg/x.php", '/')));
                            

                            最终结果将始终是开头或结尾没有斜线且内部没有双斜线的路径。随意制作一个函数。

                            编辑: 这是上面 sn-p 的一个很好的灵活函数包装器。您可以根据需要传递任意数量的路径 sn-ps,可以作为数组或单独的参数:

                            function joinPaths() {
                                $args = func_get_args();
                                $paths = array();
                                foreach ($args as $arg) {
                                    $paths = array_merge($paths, (array)$arg);
                                }
                            
                                $paths = array_map(create_function('$p', 'return trim($p, "/");'), $paths);
                                $paths = array_filter($paths);
                                return join('/', $paths);
                            }
                            
                            echo joinPaths(array('my/path', 'is', '/an/array'));
                            //or
                            echo joinPaths('my/paths/', '/are/', 'a/r/g/u/m/e/n/t/s/');
                            

                            :o)

                            【讨论】:

                            • 函数 pj($a,$b) { return rtrim($a, '/') .'/'. ltrim($b, '/'); }
                            • 这并不总是像描述的​​那样工作。 joinPaths('', 'foo.jpg') 变成 '/foo.jpg'。在我的 php 文件管理器开始将用户上传的文件写入文件系统的根目录后,我注意到了这一点!更正后的版本应删除所有空字符串路径。
                            • 是否应该使用 DIRECTORY_SEPARATOR 而不是 '/'?
                            • @fe_ 这与问题所问的功能完全不同。
                            • 我不同意,因为该人明确告知他曾经使用 python 的os.path.join 给出的结果,它给出了这个结果并且他觉得很好。所以我不相信它是另一个功能。正如 join('/a/b','../c') 应该返回 /a/c 而无需任何外部规范化。
                            【解决方案18】:
                            function join_paths() {
                                $paths = array();
                            
                                foreach (func_get_args() as $arg) {
                                    if ($arg !== '') { $paths[] = $arg; }
                                }
                            
                                return preg_replace('#/+#','/',join('/', $paths));
                            }
                            

                            我的解决方案更简单,更类似于 Python os.path.join 的工作方式

                            考虑这些测试用例

                            array               my version    @deceze      @david_miller    @mark
                            
                            ['','']             ''            ''           '/'              '/'
                            ['','/']            '/'           ''           '/'              '/'
                            ['/','a']           '/a'          'a'          '//a'            '/a'
                            ['/','/a']          '/a'          'a'          '//a'            '//a'
                            ['abc','def']       'abc/def'     'abc/def'    'abc/def'        'abc/def'
                            ['abc','/def']      'abc/def'     'abc/def'    'abc/def'        'abc//def'
                            ['/abc','def']      '/abc/def'    'abc/def'    '/abc/def'       '/abc/def'
                            ['','foo.jpg']      'foo.jpg'     'foo.jpg'    '/foo.jpg'       '/foo.jpg'
                            ['dir','0','a.jpg'] 'dir/0/a.jpg' 'dir/a.jpg'  'dir/0/a.jpg'    'dir/0/a.txt'
                            

                            【讨论】:

                            • 这是最佳答案,因为它与问题最匹配 - 它最接近 os.path.join 并且会智能地加入路径字符串。可以通过添加 os.path.join 的“参考”实现来改进答案,并指出违反规则的 OP 的具体情况(测试用例 ['abc','/def'] 是错误的 wrt os.path.join,但根据问题是正确的)。
                            • @qix 为什么? Windows 可以很好地理解正斜杠
                            • @qix 看,我明白你的意思,通常我会同意你的观点,但实际上 PHP 不会很快在不使用斜杠作为路径分隔符的平台上运行,并且使用常量 preg_replace() 会变得一团糟(你需要在正则表达式中转义路径分隔符),所以我选择了这个权衡。
                            • 所以你偷工减料是因为你很懒惰,尽管 PHP 中常量的字符串插值几乎是免费的?啧啧。
                            • 请注意,我的团队刚刚发现/ 分隔符不起作用的情况(使用 msys git shell 在 Windows Server 2012 上安装旧的 PHP5.3.4)。
                            【解决方案19】:

                            另一种攻击方式:

                            function joinPaths() {
                              $paths = array_filter(func_get_args());
                              return preg_replace('#/{2,}#', '/', implode('/', $paths));
                            }
                            

                            【讨论】:

                              【解决方案20】:

                              另一种方法是使用implode()explode()

                              $a = '/a/bc/def/';
                              $b = '/q/rs/tuv/path.xml';
                              
                              $path = implode('/',array_filter(explode('/', $a . $b)));
                              
                              echo $path;  // -> a/bc/def/q/rs/tuv/path.xml
                              

                              【讨论】:

                              • 试试网址/offset/0/limit/1.
                              【解决方案21】:

                              @deceze 的函数在尝试加入以 Unix 绝对路径开头的路径时不保留前导 /,例如joinPaths('/var/www', '/vhosts/site');.

                              function unix_path() {
                                $args = func_get_args();
                                $paths = array();
                              
                                foreach($args as $arg) {
                                  $paths = array_merge($paths, (array)$arg);
                                }
                              
                                foreach($paths as &$path) {
                                  $path = trim($path, '/');
                                }
                              
                                if (substr($args[0], 0, 1) == '/') {
                                  $paths[0] = '/' . $paths[0];
                                }
                              
                                return join('/', $paths);
                              }
                              

                              【讨论】:

                                【解决方案22】:

                                这是 deceze 发布的函数的更正版本。没有这个改变,joinPaths('', 'foo.jpg') 变成 '/foo.jpg'

                                function joinPaths() {
                                    $args = func_get_args();
                                    $paths = array();
                                    foreach ($args as $arg)
                                        $paths = array_merge($paths, (array)$arg);
                                
                                    $paths2 = array();
                                    foreach ($paths as $i=>$path)
                                    {   $path = trim($path, '/');
                                        if (strlen($path))
                                            $paths2[]= $path;
                                    }
                                    $result = join('/', $paths2); // If first element of old path was absolute, make this one absolute also
                                    if (strlen($paths[0]) && substr($paths[0], 0, 1) == '/')
                                        return '/'.$result;
                                    return $result;
                                }
                                

                                【讨论】:

                                  【解决方案23】:

                                  为了获取部分路径,您可以使用 pathinfo http://nz2.php.net/manual/en/function.pathinfo.php

                                  加入@deceze 的回复看起来不错

                                  【讨论】:

                                    猜你喜欢
                                    • 2022-08-17
                                    • 2020-11-27
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 1970-01-01
                                    相关资源
                                    最近更新 更多