首页 > 解决方案 > 将 Json 输出转换为 UTF8

问题描述

目前,我正在尝试将我的 JSON 输出转换为 UTF8,我正在使用charset=utf-8但仍然无法正常工作。我不知道为什么会这样,因为我将 charset 设置为 utf-8。此外,我的link输出包含\,例如:http:\/\/127.0.0.1\/freela\/music\/\/SERVER\/Titas - Epitafio.mp3。如何避免?

你可以在这里查看:http: //ntcdn.stream/audio/abc.php

<?php
header('Content-type: text/html; charset=utf-8');
ini_set("display_errors", 1);


function getList() {
    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]/";
    $log_directory = "SERVER/";
    $main_array = [];

    $i = 1;
    foreach(glob($log_directory.'*.*') as $file) {
        $file = str_replace($log_directory, "", $file);
        $file = str_replace(".mp3", "", $file);

        if (strpos(utf8_encode($file), '-') !== false) {
            $string = explode('-', utf8_encode($file));
            $desc = $string[0];
        } else {
            $desc = "";
        }

        $main_array[] = array(
            'id'=>$i,
            'name'=>$file,
            'description'=>trim($desc),
            'link'=>$actual_link.$log_directory.utf8_encode($file).'.mp3',
        );
        $i++;
    }
    $out = array_values($main_array);
    echo(json_encode($out)); 
}
getList();

?>

我希望你明白我在这里想说什么。谢谢!

标签: phpjson

解决方案


Instead of json_encode($out), try:

json_encode(out, JSON_UNESCAPED_UNICODE |  JSON_UNESCAPED_SLASHES)

See: https://secure.php.net/json_encode


推荐阅读