我们在运用
关于字符串的逆序排列,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()的具体用法,以及另外三种逆序排列的实现方法。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

Win11系统下如何显示文件后缀?详细解读在Windows11操作系统中,文件后缀是指文件名后面的点及其后面的字符,用来表示文件的类型。在默认情况下,Windows11系统会隐藏文件的后缀,这样在文件资源管理器中只能看到文件的名称而无法直观地了解文件的类型。然而,对于一些用户来说,显示文件后缀是非常必要的,因为它能帮助他们更好地辨识文件类型以及进行相关操

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

随着互联网的不断发展,人们越来越离不开浏览器。而在浏览器中,大家都会或多或少用到cookie这个东西。然而,很多人并不知道cookie数据在哪个文件夹中,今天就来详细解读一下。首先,我们需要了解cookie是什么。简单来说,cookie是由浏览器存储的一段文本信息,用于保存用户在浏览器中的一些个人设置或者记录用户的历史操作等等。当用户再次打开同一个网站时,c

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

转化方法:1、使用“mb_substr($url,stripos($url,"?")+1)”获取url的参数部分;2、使用“parse_str("参数部分",$arr)”将参数解析到变量中,并传入指定数组中,变量名转为键名,变量值转为键值。

去除方法:1、使用substr_replace()函数将首位数字替换为空字符串即可,语法“substr_replace($num,"",0,1)”;2、用substr截取从第二位数字开始的全部字符即可,语法“substr($num,1)”。

LinuxBashrc是Linux系统中的一个配置文件,用于设置用户的Bash(BourneAgainShell)环境。Bashrc文件存储了用户登录时所需的环境变量、启动脚本等信息,可以定制化用户的Shell环境。在Linux系统中,每个用户都有一个对应的Bashrc文件,位于用户的家目录下的隐藏文件夹中。Bashrc文件的作用主要有以下几点:设置环


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
