Skip to content

Commit

Permalink
Merge pull request #10 from GodsMercy/master
Browse files Browse the repository at this point in the history
添加JSON_UNESCAPED_UNICODE参数支持
  • Loading branch information
xtgxiso authored Oct 14, 2017
2 parents cf9a91c + 9c9d801 commit b802430
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions App.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,41 @@ public function onClientMessage($connection,$data){

}

public function ServerJson($data){


// 添加默认参数 json_encode的JSON_UNESCAPED_UNICODE参数 使之支持中文 不转义
public function ServerJson($data,$option=JSON_UNESCAPED_UNICODE){
Http::header("Content-type: application/json");
$this->conn->send(json_encode($data));
//传入0 不做处理
if ($option==0){
$this->conn->send(json_encode($data));
}else{

if ($option==JSON_UNESCAPED_UNICODE) {
// json_encode的JSON_UNESCAPED_UNICODE参数 PHP版本必须是5.4以上
if (version_compare(PHP_VERSION,'5.4.0','>=')) {
$this->conn->send(json_encode($data,$option));
}else{
// PHP版本小于5.3的支持 未经测试

// 实现方法一 如下 实现方法二 可以使用urldecode函数
// $str = json_encode($data);
// $str = preg_replace_callback("#\\\u([0-9a-f]{4})#i",function($matchs){
// iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
// },$str);
// $this->conn->send($str);
$this->conn->send(json_encode($data));
}
}

// 如果传入了别的参数
$this->conn->send(json_encode($data),$option);


}
}


public function ServerHtml($data){
$this->conn->send($data);
}
Expand Down

0 comments on commit b802430

Please sign in to comment.