字符串:$str = "abcdefg";
方法一(直接使用php自带函数strrev($str))
print_r(strrev($str));
使用for循环方式,str_split($str)
1 2 3 4 5 6 7 8 9 10 | $newArrOne = [];
$newStrOne = '';
$newArrOne = str_split ( $str );
$arrCount = count ( $newArrOne );
for ( $i =0; $i < $arrCount ; $i ++) {
$newStrOne .= $newArrOne [ $i ];
}
echo "<pre class=" brush:php;toolbar:false ">" ;
print_r( $newStrOne );
echo "
|
";
Salin selepas log masuk
使用for循环方式,strlen($substr)
1 2 3 4 5 6 7 8 | $newStrTwo = '';
$arrCountTwo = strlen ( $str );
for ( $i =1; $i <= $arrCountTwo ; $i ++) {
$newStrTwo .= substr ( $str , - $i , 1);
}
echo "<pre class=" brush:php;toolbar:false ">" ;
print_r( $newStrTwo ). "\n" ;
echo "
|
";
Salin selepas log masuk
使用for循环方式,strlen($substr)
1 2 3 4 5 6 7 8 | $newStrThree = '';
$arrCountThree = strlen ( $str );
for ( $i = $arrCountThree ; $i >=0; $i --) {
@ $newStrThree .= $str [ $i ];
}
echo "<pre class=" brush:php;toolbar:false ">" ;
print_r( $newStrThree ). "\n" ;
echo "
|
";
Salin selepas log masuk
更多php中实现字符串翻转相关文章请关注PHP中文网!