What is the function that implements string flipping in php5?

青灯夜游
Release: 2023-03-13 13:58:01
Original
1914 people have browsed it

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)".

What is the function that implements string flipping in php5?

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)
Copy after login

Return value: Returns the reversed string.

Example:

<?php
echo strrev("Hello World!");
?>
Copy after login

What is the function that implements string flipping in php5?

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  = &#39;&#39;;
        //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;
Copy after login

Output effect:

What is the function that implements string flipping in php5?

2 , using recursion

    function recursionStrrev($str){
        if (strlen($str) <= 1) return $str;//递归出口
 
        $newstr = &#39;&#39;;
        //递归点,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
Copy after login

Output effect:

What is the function that implements string flipping in php5?

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template