Make PHP pathinfo() return the correct filename if the filename is UTF-8

后端 未结 7 865
庸人自扰
庸人自扰 2020-12-06 04:07

When using PHP\'s pathinfo() function on a filename known to be UTF-8, it does not return the correct value, unless there are \'normal\' characters in front of

7条回答
  •  遥遥无期
    2020-12-06 04:38

    I have used these functions in PHP 5.3.3 - 5.3.18 to handle UTF-8 issue in basename() and pathinfo().

    
    if (!function_exists("mb_basename"))
    {
      function mb_basename($path)
      {
        $separator = " qq ";
        $path = preg_replace("/[^ ]/u", $separator."\$0".$separator, $path);
        $base = basename($path);
        $base = str_replace($separator, "", $base);
        return $base;
      }
    }
    
    if (!function_exists("mb_pathinfo"))
    {
      function mb_pathinfo($path, $opt = "")
      {
        $separator = " qq ";
        $path = preg_replace("/[^ ]/u", $separator."\$0".$separator, $path);
        if ($opt == "") $pathinfo = pathinfo($path);
        else $pathinfo = pathinfo($path, $opt);
    
        if (is_array($pathinfo))
        {
          $pathinfo2 = $pathinfo;
          foreach($pathinfo2 as $key => $val)
          {
            $pathinfo[$key] = str_replace($separator, "", $val);
          }
        }
        else if (is_string($pathinfo)) $pathinfo = str_replace($separator, "", $pathinfo);
        return $pathinfo;
      }
    }
    

提交回复
热议问题