Heim >Backend-Entwicklung >PHP-Tutorial >解读PHP函数strrev()以外的三种逆序排列方法_PHP教程
我们在运用
关于字符串的逆序排列,PHP函数strrev()的测试代码如下:
<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 class="tag-name">p</span><span class="tag">></span><span class="tag"><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> /p</span><span class="tag">></span><span>'; </span></span></span></span> </li> <li> <span>print '</span><span class="tag"><span> </span><span class="tag-name">p</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">strong</span><span class="tag">></span><span>After reversed: </span><span class="tag"><span> /strong</span><span class="tag">></span><span>'.strrev($str).'</span><span class="tag"><span> /p</span><span class="tag">></span><span>'; </span></span></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>
如果不用内置的PHP函数strrev(),又该如何实现呢?这里试验了3种方法(二分法、循环法、递归法),但没有进行性能测试。
1、二分法
<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>$mid; $i++) { </span></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、循环法
<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>=strlen($str); $i++) { </span></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、递归法
<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>
以上就是PHP函数strrev()的具体用法,以及另外三种逆序排列的实现方法。