首页 > php教程 > php手册 > 正文

解读PHP函数strrev()以外的三种逆序排列方法

php中文网
发布: 2016-06-13 11:09:17
原创
1328人浏览过

我们在运用

关于字符串的逆序排列,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><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>
登录后复制


如果不用内置的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><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、循环法

立即学习PHP免费学习笔记(深入)”;

<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、递归法

<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()的具体用法,以及另外三种逆序排列的实现方法。


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新文章
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号