=0;$i--){$newstr "/> =0;$i--){$newstr ">

Home >Backend Development >PHP Tutorial >PHP怎么实现字符串翻转

PHP怎么实现字符串翻转

WBOY
WBOYOriginal
2016-06-13 10:18:441012browse

PHP如何实现字符串翻转?
有没有现有的函数?

------解决方案--------------------
strrev()
或者
$str = "abcdefg";

function strrevv($str)
{
$len=strlen($str);
$newstr = '';
for($i=$len;$i>=0;$i--)
{
$newstr .= $str{$i};
}
return $newstr;
}
$showstr = strrevv($str);
echo $showstr."
";
------解决方案--------------------
 header("content-type:text/html;charset=utf-8");
 /*
此函数的作用是反转中文字符串
mb_strlen() 获取字符的长度
mb_substr() 获取字符的单个元素
krsort() 按照键值逆序排序数组
implode() 将数组拼接为字符串
explode() 使用字符串分隔字符串
 
 */
 
 function str_rev_gb($str){
/*判断输入的是不是utf8类型的字符,否则退出
 */
if(!is_string($str)||!mb_check_encoding($str,'UTF-8')){
exit("输入类型不是UTF8类型的字符串");
}
$array=array();
/*将字符串存入数组
 */
$l=mb_strlen($str,'UTF-8');
for($i=0;$i $array[]=mb_substr($str,$i,1,'UTF-8');
}
/*反转字符串
 */
krsort($array);
//print_r($array);
/*拼接字符串
 */
$string=implode($array);
return $string;
 }
 
 $str="lucy,请我我吃饭吧,今天我高兴";
 //$str=12345;
 //$str="hello,beautiful!";
 
 echo str_rev_gb($str);
------解决方案--------------------
http://jiarry.bokee.com/6043558.html 还能解决汉字反转问题

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