Json格式编码阿拉伯语

时间:2016-02-18 22:33:53

标签: php mysql json arabic-support

我正在使用MySQL数据库和整理utf8_general_ci,我想从这个数据库中检索一些数据,这些数据用阿拉伯语保存,但我得到这样的结果:

{"newstitle":"??????? ????? ?? ???? ???? ???? ??? ???? ????? ???? ??? ?? ???? ?????? ???????\r\n","image":"http:\/\/new.el-ahly.com\/Admin\/Sitemanager\/ArticleFiles\/83537-ron.jpg","newstext":"????? ???? ???? ????? ???????? ?? ????? ?????? ???? ???????? ?? ???? ??????? ??? ?????? ?? ??? ???16 ?????? ???? ????? ??????? ????? ????? ????????? ????????? ??????? ???? ???? ????? ?? ????????? ???????? ??? ????? ??? ???? ?????? ??????? ???? ????? ???? ??????? ?? ?????? .\r\n \r\n??? ??????? ??? ??? ???????? ????? ??? ????? ??? ??? ??????? ?????? ?? ?????? ???? ????? ???? ???? ??????? ?????? ??? ?????? ?????? ????? ?? ????? ??? ???? ???????? ??? ??? ??? ???? ????? ??? ???? ???? ?????? ???? ?? ????","time_stamp":"about 5 minutes ago","news_url":"https:\/\/goo.gl\/U33b0h"} 

这是我的php脚本:

<?php
function timeago( $ptime )
{
    $estimate_time = time() - $ptime;

    if( $estimate_time < 1 )
    {
        return 'less than 1 second ago';
    }

    $condition = array( 
                12 * 30 * 24 * 60 * 60  =>  'year',
                30 * 24 * 60 * 60       =>  'month',
                24 * 60 * 60            =>  'day',
                60 * 60                 =>  'hour',
                60                      =>  'minute',
                1                       =>  'second'
    );

    foreach( $condition as $secs => $str)
    {
        $d = $estimate_time / $secs;

        if( $d >= 1 )
        {
            $r = round( $d );
            return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
        }
    }
}

$servername = "localhost";
$username = "root";
$password = "";
$db = "finaldatabase";

$conn = mysqli_connect($servername, $username, $password, $db);

if(!$conn)
{die ( "Connection failed");}

$query = "select * from playernews";

$result = mysqli_query($conn, $query);
$rows = array();

while($row = mysqli_fetch_assoc($result)) {
    $News_Titles[]=$row['NewsTitle'];
    $Images[]=$row['image'];
    $News_Text[]=$row['NewsText'];
    $Time_Stamp[]=$row['timeStamp'];
    $News_Url[]=$row['url'];

    //echo $row['url'];
}

// sleep for 2 sec show that the androd swipe refresh will be visible for sometime
sleep(2);

// reading offset from get parameter
$offset = isset($_GET['offset']) && $_GET['offset'] != '' ? $_GET['offset'] : 0;

// page limit
$limit = 20;
//$offset=0;


$movies_array = array();

//$rows=array_reverse($rows);

// loop through page movies
for ($j = $offset; $j < $offset + $limit && $j < sizeof($Images); $j++) {
    $tmp = array();
    //$tmp['rank'] = $j + 1;
    $tmp['newstitle'] = $News_Titles[$j];
    $tmp['image']=$Images[$j];
    $tmp['newstext']=$News_Text[$j];
    $time_ago =strtotime($Time_Stamp[$j]);

    $tmp['time_stamp']=timeAgo($time_ago);
    $tmp['news_url']=$News_Url[$j];

    array_push($movies_array, $tmp);
}

//$reverse = array_reverse($movies_array, true);

// printing json response
echo json_encode($movies_array);

 $curenttime="2016-02-18 22:29:13";
  $time_ago =strtotime($curenttime);
  //echo timeAgo($time_ago);

$myfile = fopen("movies.json", "w") or die("Unable to open file!");
$txt = json_encode($movies_array);
fwrite($myfile, $txt);
fclose($myfile);

//header("Location: movies.json"); /* Redirect browser */
exit();
?>

我应该怎么处理这个问题?

1 个答案:

答案 0 :(得分:0)

$conn=mysqli_connect($servername,$username,$password,$db);
$this->mysqli->set_charset('utf8mb4');  // add this line

utf8_general_ci也很糟糕,请改用utf8mb4_unicode_ci