Home >php教程 >php手册 >php实现中文字符串翻转

php实现中文字符串翻转

WBOY
WBOYOriginal
2016-06-06 19:53:031543browse

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 第一种方法: function cstrrev($str) { $len = strlen($str); for($i = 0; $i $len; $i++) { $char = $str{0}; if(ord($char) 127) { $i++; if($i $len) { $arr[] = substr($str, 0,

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  第一种方法:

  function cstrrev($str)

  {

  $len = strlen($str);

  for($i = 0; $i

  {

  $char = $str{0};

  if(ord($char) > 127)

  {

  $i++;

  if($i

  {

  $arr[] = substr($str, 0, 2);

  $str = substr($str, 2);

  }

  }

  else

  {

  $arr[] = $char;

  $str = substr($str, 1);

  }

  }

  return join(array_reverse($arr));

  }

  第二种方法:

  function cstrrev($str)

  {

  $len = mb_strlen($str,'UTF-8');

  $str_arr = array();

  for($i=0; $i

  $str_arr[$i] = mb_substr($str,$i,1,'UTF-8');//拆成单字到组

  }

  return join(array_reverse($str_arr));

  }

php实现中文字符串翻转

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