Home > Backend Development > PHP Tutorial > Interpret three reverse order methods other than PHP function strrev()_PHP Tutorial

Interpret three reverse order methods other than PHP function strrev()_PHP Tutorial

WBOY
Release: 2016-07-15 13:32:15
Original
1382 people have browsed it

We are using

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

<ol class="dp-xml">
<li class="alt"><span><span>header('Content-type: text/html; </span><span class="attribute">charset</span><span>=</span><span class="attribute-value">utf</span><span>-8');  </span></span></li>
<li>
<span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">implode</span><span>('', range(9, 0));  </span>
</li>
<li class="alt">
<span>print '</span><span class="tag"><</span><span> </span><span class="tag-name">p</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">strong</span><span class="tag">></span><span>Before reversed: </span><span class="tag"></</span><span class="tag-name">strong</span><span class="tag">></span><span>'.$str.'</span><span class="tag"><</span><span> /p</span><span class="tag">></span><span>';  </span>
</li>
<li>
<span>print '</span><span class="tag"><</span><span> </span><span class="tag-name">p</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">strong</span><span class="tag">></span><span>After reversed: </span><span class="tag"><</span><span> /strong</span><span class="tag">></span><span>'.strrev($str).'</span><span class="tag"><</span><span> /p</span><span class="tag">></span><span>';  </span>
</li>
<li class="alt"><span>/*  </span></li>
<li><span>输出如下:  </span></li>
<li class="alt"><span>Before reversed: 9876543210  </span></li>
<li><span>After reversed: 0123456789  </span></li>
<li class="alt"><span>*/ </span></li>
</ol>
Copy after login


How to implement this without using the built-in PHP function strrev()? Three methods (bisection method, loop method, recursive method) were tested here, but no performance test was conducted.

1. Dichotomy

<ol class="dp-xml">
<li class="alt"><span><span>/**  </span></span></li>
<li><span>* 二分法实现字符串逆序排列  </span></li>
<li class="alt"><span>* @param string $str 源字符串  </span></li>
<li><span>* @return string 返回逆序后的字符串  </span></li>
<li class="alt"><span>*/  </span></li>
<li>
<span>function reverse($</span><span class="attribute">str</span><span>=</span><span class="attribute-value">''</span><span>) {  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">len</span><span> = </span><span class="attribute-value">strlen</span><span>($str);//不能使用count或sizeof  </span>
</li>
<li>
<span>$</span><span class="attribute">mid</span><span> = </span><span class="attribute-value">floor</span><span>($len/2);  </span>
</li>
<li class="alt">
<span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>; $i</span><span class="tag"><</span><span>$mid; $i++) {  </span></li><li><span>$</span><span class="attribute">temp</span><span> = $str[$i];  </span></li><li class="alt"><span>$str[$i] = $str[$len-$i-1];  </span></li><li><span>$str[$len-$i-1] = $temp;  </span></li><li class="alt"><span>}  </span></li><li><span>return $str;  </span></li><li class="alt"><span>} </span></li></ol>
Copy after login


2. Circular method

<ol class="dp-xml"><li class="alt"><span><span>/**  </span></span></li><li><span>* 循环实现对字符串的逆序排列(效率比二分法低)  </span></li><li class="alt"><span>* @param string $str 源字符串  </span></li><li><span>* @return string 返回逆序后的字符串  </span></li><li class="alt"><span>*/  </span></li><li><span>function reverse($</span><span class="attribute">str</span><span>=</span><span class="attribute-value">''</span><span>) {  </span></li><li class="alt"><span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">''</span><span>;  </span></li><li><span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">1</span><span>; $i</span><span class="tag"><</span><span>=strlen($str); $i++) {  </span></li><li class="alt"><span>$result </span><span class="attribute">.</span><span>= </span><span class="attribute-value">substr</span><span>($str, -$i, 1);  </span></li><li><span>}  </span></li><li class="alt"><span>return $result;  </span></li><li><span>}  </span></li></ol>
Copy after login

3. Recursive method

<ol class="dp-xml"><li class="alt"><span><span>/**  </span></span></li><li><span>* 递归实现对字符串的逆序排列(效率低)  </span></li><li class="alt"><span>* @param string $str 源字符串  </span></li><li><span>* @return string 返回逆序后的字符串  </span></li><li class="alt"><span>*/  </span></li><li><span>function reverse($</span><span class="attribute">str</span><span>=</span><span class="attribute-value">''</span><span>) {  </span></li><li class="alt"><span>static $</span><span class="attribute">result</span><span> = </span><span class="attribute-value">''</span><span>;  </span></li><li><span>/* 用堆栈来理解递归调用 */  </span></li><li class="alt"><span>if (strlen($str) </span><span class="tag">></span><span> 0) {  </span>
</li>
<li><span>reverse(substr($str, 1));  </span></li>
<li class="alt">
<span>$result </span><span class="attribute">.</span><span>= </span><span class="attribute-value">substr</span><span>($str, 0, 1);//此句必须放在上一语句之后  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>return $result;  </span></li>
<li><span>}  </span></li>
</ol>
Copy after login

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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446151.htmlTechArticleWe are using the reverse order of strings. The test code of the PHP function strrev() is as follows: header('Content -type:text/html; charset = utf -8'); $ str = implode ('',range(9,0)); prin...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template