我在PHP 5.3中遇到json_encode
和class example {
public static function setHttpHeader($httpStatusCode, $httpStatusMsg){
$phpSapiName = substr(php_sapi_name(), 0, 3);
if ($phpSapiName == 'cgi' || $phpSapiName == 'fpm') {
header('Status: '.$httpStatusCode.' '.$httpStatusMsg);
} else {
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
header($protocol.' '.$httpStatusCode.' '.$httpStatusMsg);
}
}
public static function sendJsonReponse($http_status_code, $http_status_msg, $response = false){
$json = array();
$json['meta'] = array(
"status" => $http_status_code,
"message" => $http_status_msg,
);
if($response){
$json['response'] = $response;
}
echo json_encode($json);
self::setHttpHeader($http_status_code, $http_status_msg);
die;
}
}
非常奇怪的错误。我有一个发送json响应的函数:
\n
一切正常,期待我在回复时遇到奇怪的\t
和preg_replace
字符串:http://pastebin.com/P0UbjLEn
当我想使用if($response){
$json['response'] = preg_replace("@[\\r|\\n|\\t]+@", "", $response);
}
来逃避它们时:
$reponse
所有我得到的东西看起来都是好的JSON消息,但它没有,我无法理解:http://pastebin.com/GSh4M7X1
我的simctl
是SMARTY收集的HTML代码。