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>
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>
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>
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>
The above is the specific usage of the PHP function strrev(), and the other three implementation methods of reverse order.