简体   繁体   中英

Convert Json output to UTF8

Currently, I'm trying convert my JSON output to UTF8, I'm using charset=utf-8 but still not working. I don't know why it's happening since I set charset to utf-8. Also, my link output contains \\ , like: http:\\/\\/127.0.0.1\\/freela\\/music\\/\\/SERVER\\/Titas - Epitafio.mp3 . How to avoid it?

You can check it here: 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();

?>

I hope that you understand what I'm trying to say here. Thank you!

Instead of json_encode($out) , try:

json_encode(out, JSON_UNESCAPED_UNICODE |  JSON_UNESCAPED_SLASHES)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM