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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

With the popularity of cryptocurrencies, virtual currency trading platforms have emerged. The top ten virtual currency trading platforms in the world are ranked as follows according to transaction volume and market share: Binance, Coinbase, FTX, KuCoin, Crypto.com, Kraken, Huobi, Gate.io, Bitfinex, Gemini. These platforms offer a wide range of services, ranging from a wide range of cryptocurrency choices to derivatives trading, suitable for traders of varying levels.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How to adjust Sesame Open Exchange to Chinese? This tutorial covers detailed steps on computers and Android mobile phones, from preliminary preparation to operational processes, and then to solving common problems, helping you easily switch the Sesame Open Exchange interface to Chinese and quickly get started with the trading platform.

The top ten cryptocurrency trading platforms include: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op
