We are using
For the reverse order of strings, the PHP function Strrev () has the following test code:
- header (' content-type:text/html; CharSet = UTF -8 ');
- $ Str = implode (', Range (9, 0));
- print ' < p><strong>before reversed: strong>'. $str. '< / p> ';
- print ' < p> < strong>afterreversed: < /strong>'. Strrev ($str). ' < / P > ';
- /*
- The output is as follows:
- Before reversed:9876543210
- After reversed:0123456789
- */
What if we don't use the built-in PHP function Strrev ()? 3 Methods (dichotomy, cyclic, recursive) were tested, but no performance tests were performed.
1. Dichotomy
- /**
- * Binary method for string reverse order
- * @param string $STR source strings
- * @return string returned in reverse order
- */
- function Reverse ($str=") {
- $ Len = strlen ($STR);//cannot use count or sizeof
- $ Mid = Floor ($len/2);
- For ($i=0; $i<$mid; $i + +) {
- $ Temp = $str [$i];
- $STR [$i] = $str [$len-$i-1];
- $str [$len-$i-1] = $temp;
- }
- return $str;
- }
2. Cyclic method
- /**
- * Cyclic implementation of strings in reverse order (efficiency is lower than dichotomy)
- * @param string $STR source strings
- * @return string returned in reverse order
- */
- function Reverse ($str=") {
- $ result = '' ;
- For ($i=1; $i<=strlen ($STR); $i + +) {
- $result . = substr ($str,-$i, 1);
- }
- return $result;
- }
3. Recursive method
- /**
- * Recursive implementation of strings in reverse order (inefficient)
- * @param string $STR source strings
- * @return string returned in reverse order
- */
- function Reverse ($str=") {
- Static $ result = '' ;
- /* Use the stack to understand recursive calls */
- if (strlen ($str) > 0) {
- Reverse (substr ($STR, 1));
- $result . = substr ($str, 0, 1);//The sentence must be placed after the previous statement
- }
- return $result;
- }
This is the specific use of PHP function Strrev (), as well as three other ways to implement the reverse order.
http://www.bkjia.com/PHPjc/446151.html www.bkjia.com true http://www.bkjia.com/PHPjc/446151.html techarticle we are using the reverse order of the string, PHP function Strrev () The test code is as follows: Header (' content-type:text/html; charset = Utf-8 '); $ str = implode (", Range ( 9,0)); Prin ...