preg_replace和json_encode之后的无效JSON,\ n和\ t

时间:2015-11-06 16:09:49

标签: php json regex smarty

我在PHP 5.3中遇到json_encodeclass 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

一切正常,期待我在回复时遇到奇怪的\tpreg_replace字符串:http://pastebin.com/P0UbjLEn 当我想使用if($response){ $json['response'] = preg_replace("@[\\r|\\n|\\t]+@", "", $response); } 来逃避它们时:

$reponse

所有我得到的东西看起来都是好的JSON消息,但它没有,我无法理解:http://pastebin.com/GSh4M7X1

我的simctl是SMARTY收集的HTML代码。

0 个答案:

没有答案