Home>Article>Backend Development> What is the function that implements string flipping in php5?
php5 The function that implements string flipping is "strrev()". The function of this function is to reverse the string, reverse the order of characters in the string, and return the reversed string; syntax "strrev(string)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php5 function to implement string flipping is "strrev()".
strrev() function reverses a string.
Syntax:
strrev(string)
Return value: Returns the reversed string.
Example:
Extended knowledge:In addition to using the strrev() function to flip the string, you can also use the following two Method to flip
1. Split the string into an array, then traverse and concatenate:
function joinStrrev($str){ if (strlen($str) <= 1) return $str; $newstr = ''; //str_split(string,length) 函数把字符串分割到数组中:string 必需。规定要分割的字符串。length 可选。规定每个数组元素的长度。默认是 1。 $strarr = str_split($str,1); foreach ($strarr as $word) { $newstr = $word.$newstr; } return $newstr; } $revstr = joinStrrev($str); echo $revstr;
Output effect:
2 , using recursion
function recursionStrrev($str){ if (strlen($str) <= 1) return $str;//递归出口 $newstr = ''; //递归点,substr(string,start,length) :substr() 函数返回字符串的一部分,如果 start 参数是负数且 length 小于或等于 start,则 length 为 0,正数 - 在字符串的指定位置开始,负数 - 在从字符串结尾的指定位置开始,0 - 在字符串中的第一个字符处开始 $newstr .= substr($str,-1).recursionStrrev(substr($str,0,strlen($str)-1)); return $newstr;//递归出口 } $revstr = recursionStrrev($str); echo $revstr
Output effect:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the function that implements string flipping in php5?. For more information, please follow other related articles on the PHP Chinese website!