Home > Backend Development > PHP Tutorial > PHP json_encode Chinese processing class example_PHP tutorial

PHP json_encode Chinese processing class example_PHP tutorial

WBOY
Release: 2016-07-13 10:49:41
Original
914 people have browsed it

Chinese processing in php is always not perfect. We often encounter the problem of garbled or empty Chinese characters. Below I also encountered the same problem when processing json. Later, I compiled a perfect solution to json_encode Chinese garbled or empty characters. Empty program class.

 代码如下 复制代码

/**************************************************************
    *
    * 使用特定function对数组中所有元素做处理
    * @param string &$array 要处理的字符串
    * @param string $function 要执行的函数
    * @return boolean $apply_to_keys_also 是否也应用到key上
    * @access public
    *
    *************************************************************/
    function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
    die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
    if (is_array($value)) {
    arrayRecursive($array[$key], $function, $apply_to_keys_also);
    } else {
    $array[$key] = $function($value);
    }
    if ($apply_to_keys_also && is_string($key)) {
    $new_key = $function($key);
    if ($new_key != $key) {
    $array[$new_key] = $array[$key];
    unset($array[$key]);
    }
    }
    }
    $recursive_counter--;
    }
    /**************************************************************
    *
    * 将数组转换为JSON字符串(兼容中文)
    * @param array $array 要转换的数组
    * @return string 转换得到的json字符串
    * @access public
    *
    *************************************************************/
    function JSON($array) {
    arrayRecursive($array, 'urlencode', true);
    $json = json_encode($array);
    return urldecode($json);
    }
    
    $array = array
    (
    'Name'=>'络恩',
    'Age'=>24
    );
    
    
    echo JSON($array);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632709.htmlTechArticleChinese processing in PHP is always not perfect. We often encounter problems with garbled or empty characters when processing Chinese. I also encountered the same problem when processing json below, and later compiled a perfect solution...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template