php - Json格式编码阿拉伯语

标签 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();
?>

遇到这个问题我该怎么办?

最佳答案

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

utf8_general_ci 也很烂,改用 utf8mb4_unicode_ci

关于php - Json格式编码阿拉伯语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35493819/

相关文章:

sql - Azure ARM 模板 "VmAgentNotRunning"

jquery - 如何使用 jQuery 访问多维 JSON 数组中的值

php - 如何在 codeigniter 中引用 css 文件?

PHP、MySQL 存储过程未按预期存储数据

php - 是否可以在图像中保存元数据?

php - 数据规范化还是智能搜索?

php - 将可选空白字段添加到 php 表单

javascript - Handlebars 中的访问对象属性包含 '.'

php - 无法更新子行

php - 为什么 arsort() 会把一个中心值作为第一个呢?