Explanation of three reverse sorting methods other than the PHP function strrev () _ PHP Tutorial

Source: Internet
Author: User
Three reverse sorting methods except strrev () are interpreted. The PHP function strrev () test code is as follows: header (Content-type: texthtml; charsetutf-8); $ strimplode (, range (9,0 )); prin we are using

For the reverse sorting of strings, the test code of the PHP function strrev () is as follows:

 
 
  1. Header ('content-type: text/html; charset = utf-8 ');
  2. $ Str = implode ('', range (9, 0 ));
  3. Print '<p>Before reversed:'. $ Str.' </p> ';
  4. Print '<p> <strong> After reversed: </strong>'. strrev ($ str). '</p> ';
  5. /*
  6. The output is as follows:
  7. Beforereversed: 9876543210
  8. After reversed: 0123456789
  9. */


How can I implement strrev () without the built-in PHP function? Three methods (bipartite, cyclic, and recursive) were tested, but no performance tests were conducted.

1. binary classification

 
 
  1. /**
  2. * The binary method is used to sort strings in reverse order.
  3. * @ Param string $ str Source string
  4. * @ Return string returns the reverse string.
  5. */
  6. Function reverse ($ str = ''){
  7. $ Len = strlen ($ str); // you cannot use count or sizeof.
  8. $ Mid = floor ($ len/2 );
  9. For ($ I = 0; $ I <$ mid; $ I ++ ){
  10. $ Temp = $ str [$ I];
  11. $ Str [$ I] = $ str [$ len-$ i-1];
  12. $ Str [$ len-$ i-1] = $ temp;
  13. }
  14. Return $ str;
  15. }


2. circulation method

 
 
  1. /**
  2. * Cyclically arrange strings in reverse order (efficiency is lower than that of the binary method)
  3. * @ Param string $ str Source string
  4. * @ Return string returns the reverse string.
  5. */
  6. Function reverse ($ str = ''){
  7. $ Result = '';
  8. For ($ I = 1; $ I <= strlen ($ str); $ I ++ ){
  9. $ Result. = substr ($ str,-$ I, 1 );
  10. }
  11. Return $ result;
  12. }

3. recursion

 
 
  1. /**
  2. * Recursively arrange strings in reverse order (low efficiency)
  3. * @ Param string $ str Source string
  4. * @ Return string returns the reverse string.
  5. */
  6. Function reverse ($ str = ''){
  7. Static $ result = '';
  8. /* Use stacks to understand recursive calls */
  9. If (strlen ($ str)> 0 ){
  10. Reverse (substr ($ str, 1 ));
  11. $ Result. = substr ($ str, 0, 1); // The sentence must be placed after the previous statement.
  12. }
  13. Return $ result;
  14. }

The above is the specific usage of the PHP function strrev () and the implementation of the other three methods in reverse order.


The PHP function strrev () has the following test code: header ('content-type: text/html; charset = utf-8 '); $ str = implode ('', range (9, 0); prin...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.