Home  >  Article  >  php教程  >  php json_encode中文编码

php json_encode中文编码

PHP中文网
PHP中文网Original
2016-05-25 16:58:241568browse

php代码

/**
 * 中文编码JSON
 * @param $data
 * @return string
 */
function encode_json($data){
	if(version_compare(&#39;5.4&#39;,PHP_VERSION,&#39;<&#39;)){
		//5.4以上
		return json_encode($data,JSON_UNESCAPED_UNICODE);
	}else{
		return urldecode(json_encode(url_encode($data)));
	}
}

2. 代码 

/**
 * URL编码(可迭代数组)
 * @param $str
 * @return array|string
 */
function url_encode($str) {
	if(is_array($str)) {
		foreach($str as $key=>$value) {
			$str[urlencode($key)] = url_encode($value);
		}
	} else {
		$str = urlencode($str);
	}

	return $str;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn