duanlei5339 2016-10-02 11:32
浏览 897

在json_encode中传递�或ü

I have created a function that grab the title from the given URL and return json. The function is call by ajax. All works fine but when any title contain the ü or any related character it return null. I have echo the string in Network tab it shows � instead of character's like ü.

I have tried the solution I get from the internet like to use JSON Constant , solution[1] , solution[2], solution[3] and many



After I tried htmlentities but it return null string



Code:

$value = array();
$val['title'] = get_title("URL");
$val['link']  = $line;
array_push($value, $val);

function get_title($url)
{

    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    $query = curl_exec($curl_handle);
    curl_close($curl_handle);

        $normalizeChars = array(
            'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
            'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
            'Ï'=>'I', 'Ñ'=>'N', 'Ń'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
            'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
            'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
            'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
            'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ü' => 'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f',
            'ă'=>'a', 'î'=>'i', 'â'=>'a', 'ș'=>'s', 'ț'=>'t', 'Ă'=>'A', 'Î'=>'I', 'Â'=>'A', 'Ș'=>'S', 'Ț'=>'T', '�' => 'u',
        );

    $query = trim(preg_replace('/\s+/', ' ', $query)); // supports line breaks inside <title>
    preg_match("/\<title\>(.*)\<\/title\>/", $query, $title); // ignore case

        $search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,ø,Ø,Å,Á,À,Â,Ä,È,É,Ê,Ë,Í,Î,Ï,Ì,Ò,Ó,Ô,Ö,Ú,Ù,Û,Ü,Ÿ,Ç,Æ,Œ,�");

 $replace = explode(",","c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,o,O,A,A,A,A,A,E,E,E,E,I,I,I,I,O,O,O,O,U,U,U,U,Y,C,AE,OE,u");

        //$title[1] = strtr($title[1], $normalizeChars); //Tried strtr function not work
        //echo str_replace($search, $replace, $title[1]); // tried replace function not work
        //echo $title[1];
        //$title[1] = htmlentities($title[1]); //works but $title[1] is null

    if (strpos($url, '/') !== false) {
        $url1 = explode('/', $url1);
        $res  = $url1[0] . ' | ' . $title[1];
    } else {
        $res = $url1 . ' | ' . $title[1];
    }
    $res = substr($res, 0, 99);
    return $res;
    }
    catch(Exception $e) { echo $e; }

}

echo json_encode($value);
  • 写回答

1条回答 默认 最新

  • dry18813 2016-10-02 12:20
    关注

    Try using JSON_UNESCAPED_UNICODE (only available for PHP >= 5.4.0):

    json_encode($value, JSON_UNESCAPED_UNICODE);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大