This article mainly introduces how to implement php string flip, which has good reference value. let's take a look at it together with the small editor.
String: $ str = "abcdefg ";
Method 1 (directly use the built-in function strrev ($ str) in php ))
print_r(strrev($str));
Use the for loop method, str_split ($ str)
$ NewArrOne = []; // initialize a new array $ newStrOne = ''; // initialize a new string $ newArrOne = str_split ($ str ); $ arrCount = count ($ newArrOne); for ($ I = 0; $ I <$ arrCount; $ I ++) {$ newStrOne. = $ newArrOne [$ I];} echo""; print_r($newStrOne); echo "
";
Use the for loop method, strlen ($ substr)
$ NewStrTwo = ''; // initialize a new string $ arrCountTwo = strlen ($ str); for ($ I = 1; $ I <= $ arrCountTwo; $ I ++) {$ newStrTwo. = substr ($ str,-$ I, 1);} echo""; print_r($newStrTwo)."\n"; echo "
";
Use the for loop method, strlen ($ substr)
$ NewStrThree = ''; // initialize a new string $ arrCountThree = strlen ($ str); for ($ I = $ arrCountThree; $ I >=0; $ I --) {@ $ newStrThree. = $ str [$ I];} echo"";print_r($newStrThree)."\n";echo "
";
For more articles about character string flip in php, refer to PHP Chinese website!