Home  >  Article  >  Backend Development  >  PHP string learning reversely outputs all characters

PHP string learning reversely outputs all characters

青灯夜游
青灯夜游Original
2021-08-10 19:02:553406browse

In the previous article "PHP String Learning: Counting the Occurrences of Characters", we introduced the method of counting the occurrences of all characters or specified characters in a string. This time we introduce the method of reversing a string and outputting all the characters of the string in reverse order. You can refer to it if you need it.

The topic introduced to you today is to reversely output a string. There are many ways to achieve this operation. We can

  • traverse the string in reverse, and then Use the character concatenation "." operator to splice the characters from back to front to splice a new string (reverse the string), and finally output it. [Recommended reading: "Teach you to use PHP operators to splice two strings together"]

  • Split the string into an array, and then traverse Array, reversely splice the array elements into a new string, and finally output it.

  • First use the function to reverse the string directly, and then output the reversed string.

Let’s introduce them one by one, first look at the following example:

<?php
function fz1($str){
    $len=strlen($str);
    $res = &#39;&#39;;
    for($m=$len-1;$m>=0;$m--){
        $res .= $str[$m];
    }
    return $res;
}
echo fz1("abcdefg");
?>

The output result is:

PHP string learning reversely outputs all characters

It can be seen that we use a for loop to reverse the order of the characters in the string "abcdefg", splice a new string "gfedcba", and then output the string.

Let’s take a look at another example:

<?php
function fz1($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;
}
echo fz1("AbCdefg");
?>

We use str_split($str,1) to split the $str string into the $strarr array; then use foreach to traverse $strarr array, use the "$newstr = $word.$newstr" statement in the loop body to reversely splice the array elements into a new string; finally output the new string.

Let’s take a look at the output results:

PHP string learning reversely outputs all characters

The above two examples are a bit complicated to reverse the string and output all the characters in reverse. In fact, PHP has a built-in function that can reverse a string in one step.

<?php
$str="AbCdefg";
echo strrev($str);
?>

Output result:

PHP string learning reversely outputs all characters

Is it very simple? You only need one line of code strrev($str) to reverse the string The order of the characters in the string is reversed.

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

Finally, I recommend reading a classic course "PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !

The above is the detailed content of PHP string learning reversely outputs all characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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