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

后端 未结 7 873
庸人自扰
庸人自扰 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:25

    When process ansi characters, the function pathinfo do correctly.

    Base this note, we will convert (encoding) input to ansi charaters and then still use function pathinfo to keep its whole things.

    Finally, we will convert (decoding) output values to original format.

    And demo as bellowing.

    function _pathinfo($path, $options = null)
    {
        $path = urlencode($path);
        $parts = null === $options ? pathinfo($path) : pathinfo($path, $options);
        foreach ($parts as $field => $value) {
            $parts[$field] = urldecode($value);
        }
        return $parts;
    }
    // calling
    _pathinfo('すtest.jpg');
    _pathinfo('すtest.jpg', PATHINFO_EXTENSION);
    

提交回复
热议问题