【问题标题】:PHP: How can copy($url, $filename) work with %-encoding for UTF-8 characters?PHP:copy($url, $filename) 如何使用 UTF-8 字符的 %-encoding?
【发布时间】:2011-03-25 00:10:34
【问题描述】:

这是怎么回事

copy("http://translate.google.com/translate_tts?tl=en&q=Love+Me", "/directory/loveme.mp3");

但这不是吗?

copy("http://translate.google.com/translate_tts?tl=hi&q=%26%232310%3B%26%232354%3B%26%232370%3B+%26%232327%3B%26%232379%3B%26%232349%3B%26%232368%3B",  "/directory/loveme.mp3");

如果我将两个 URL 都粘贴到浏览器中,它们都可以正常播放。但是第二个 URL 只是复制了一个空白的 mp3 文件,而第一个 URL 复制了正确的 MP3 文件。

【问题讨论】:

    标签: php copy mp3


    【解决方案1】:

    在调用 copy 之前尝试在 URL 上执行此操作:

    function utf8RawUrlDecode ($source) {
        $decodedStr = "";
        $pos = 0;
        $len = strlen ($source);
        while ($pos < $len) {
            $charAt = substr ($source, $pos, 1);
            if ($charAt == '%') {
                $pos++;
                $charAt = substr ($source, $pos, 1);
                if ($charAt == 'u') {
                    // we got a unicode character
                    $pos++;
                    $unicodeHexVal = substr ($source, $pos, 4);
                    $unicode = hexdec ($unicodeHexVal);
                    $entity = "&#". $unicode . ';';
                    $decodedStr .= utf8_encode ($entity);
                    $pos += 4;
                }
                else {
                    // we have an escaped ascii character
                    $hexVal = substr ($source, $pos, 2);
                    $decodedStr .= chr (hexdec ($hexVal));
                    $pos += 2;
                }
            } else {
                $decodedStr .= $charAt;
                $pos++;
            }
        }
        return $decodedStr;
    }
    

    我是从docs for rawurldecode 上的 cmets 那里得到的——向下滚动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      相关资源
      最近更新 更多