Home >Backend Development >PHP Tutorial >Loop implementation (string, object, or array) encoding and conversion in PHP_PHP tutorial

Loop implementation (string, object, or array) encoding and conversion in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:55:46794browse

The article introduces how to use traversal to achieve mutual conversion of string encodings. If you need to understand the conversion of strings, objects, or arrays, you can take a look.

The code is as follows
 代码如下 复制代码

/**
 * 循环实现编码互转
 *
 * @param string $param(字符串,对象,或者数组),$currCharset当前编码,$toCharset期望编码
 * @return 参数类型
 */
function zhandi_iconv($param,$currCharset,$toCharset){
 if ($currCharset != $toCharset){
  if (is_string($param)){
   return iconv($currCharset, $toCharset, $param);
  }
  elseif (is_array($param)){
   foreach ($param as $key => $value){
    $param[$key] = zhandi_iconv($value);
   }
   return $param;
  }
  elseif (is_object($param)){
   foreach ($param as $key => $value){
    $param->$key = zhandi_iconv($value);
   }
   return $param;
  }
  else{
   return $param;
  }
 }
 return $param;
}

Copy code
/**
* Loop to realize coding mutual conversion
*
* @param string $param (string, object, or array), $currCharset current encoding, $toCharset expected encoding
* @return parameter type
​*/
function zhandi_iconv($param,$currCharset,$toCharset){
if ($currCharset != $toCharset){
if (is_string($param)){
Return iconv($currCharset, $toCharset, $param);
}
elseif (is_array($param)){
foreach ($param as $key => $value){
$param[$key] = zhandi_iconv($value);
}
Return $param;
}
elseif (is_object($param)){
foreach ($param as $key => $value){
$param->$key = zhandi_iconv($value);
}
Return $param;
}
else{
Return $param;
}
}
return $param;
}

http://www.bkjia.com/PHPjc/631646.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631646.html
TechArticle
The article introduces how to use traversal to achieve mutual conversion of string encodings. There are strings that need to be understood. , object, or array conversion can be looked at. The code is as follows Copy...
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