一、按字节来反转字符串
1.代码:
print strrev('a nice day!'); ?>
2.输出结果:!yad ecin a
二、 按字反转字符串
1.代码:
$str = "Today is a nice day!"; //将字符串分解为独立的字 $words = exlpode(' ', $str); //注意单引号中有一个空格 //反转这个字数组 $words = array_reverse($words); //重建反转后的字符串 $str = implode(' ', $words); //注意单引号中有一个空格 print $str; ?>
2.输出结果: day! nice a is Today
3.简化后的代码:
$str = "Today is a nice day!"; $reverse_str = implode(' ', array_reverse(explode(' ', $str))); print $reverse_str; ?>