Home >Backend Development >PHP Tutorial >Detailed explanation of how to implement string flipping in PHP
This article mainly introduces the method of realizing PHP string flipping, which has a good reference value. Let’s take a look at it with the editor.
String: $str = "abcdefg";
Method 1 (directly use PHP’s built-in function strrev($str))
print_r(strrev($str));
Use the for loop method, str_split($str)
$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 "";
Use the for loop method, strlen($substr)
$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 "";
Use the for loop method, strlen($substr)
$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 "";
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
How to get the first few digits of string in php
php uses eval to implement the calculation formula in the string format
JS String method usage steps Detailed explanation
The above is the detailed content of Detailed explanation of how to implement string flipping in PHP. For more information, please follow other related articles on the PHP Chinese website!