Home > php教程 > PHP源码 > body text

php json_encode中文编码

PHP中文网
Release: 2016-05-25 16:58:24
Original
1611 people have browsed it

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)));
	}
}
Copy after login

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;
}
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template