首页  >  文章  >  后端开发  >  PHP 中的反转字符串

PHP 中的反转字符串

PHPz
PHPz原创
2024-08-29 13:12:35321浏览

在本文中,我们将了解 PHP 中的反向字符串。字符串是从背面调用的字符的集合。这些字符集合可用于反转 PHP 函数 strrev() 或使用简单的 PHP 编程代码。例如,逆转 PAVANKUMAR 将变为 RAMUKNAVAP

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

逻辑:

  • 首先,将字符串分配给变量。
  • 现在计算字符串长度。
  • 现在创建一个新变量来存储反转的字符串。
  • 然后继续循环,如 for 循环、while 循环和 do while 循环等..
  • 在循环内连接字符串。
  • 然后显示/打印存储在中间创建的新变量中的反转字符串。

使用各种循环反转 PHP 中的字符串

PHP 编程语言中用于反转字符串的各种循环,例如 FOR LOOP、WHILE LOOP、DO WHILE LOOP、RECURSIVE METHOD 等。

 1. 使用 strrev() 函数

  • 下面的示例/语法将使用已经预定义的函数 strrev() 反转原始字符串,以反转字符串。在示例中,创建了一个 $string1 变量来存储字符串(我的意思是原始字符串),然后使用 echo 语句在 strtev() 函数的帮助下打印原始字符串和反转的字符串,然后通过连接两者。
  • 您可以检查下面程序的输出来检查字符串是否颠倒。

代码:

<?php
$string1 = "PAVANKUMAR";
echo "Reversed string of the $string1 is " .strrev ( $string1 );
?>

输出:

PHP 中的反转字符串

2.使用 For 循环

  • 在下面的示例中,“$string1”变量是通过将 string1 变量的值分配为“PAVANSAKE”来创建的。然后创建 $length 变量,以便使用 strlen() 函数存储 $string1 变量的长度。现在,for 循环与初始化、条件和增量值一起使用,如“$length1-1”、“$i1>=0”、“$i1 – -”。然后 $string1 变量的索引值从后端调用,使用 $i1 的初始化等于 $length-1。
  • 首先,FOR LOOP将从“原始字符串-1”值的长度值开始。然后循环通过检查条件“i1>=0”开始运行,然后向后调用原始字符串的索引,同样循环将从每次迭代的后面打印每个索引值,直到条件为 FALSE。最后,我们将使用 FOR 循环得到字符串的反转。

代码:

<?php
$string1 = "PAVANSAKE";
$length1 = strlen($string1);
for ($i1=($length1-1) ; $i1 >= 0 ; $i1--)
{
echo $string1[$i1];
}
?>

输出:

PHP 中的反转字符串

3.使用 While 循环

  • 在下面的示例中,使用 while 循环来打印使用原始字符串“PAVANSAKEkumar”反转的字符串。
  • 在下面的syntax/php程序中,首先,为$string1变量分配字符串值“PAVANSAKEkumar”,然后我们计算$string1变量值的长度并将其存储在$length1变量中。它将始终是一个整数。现在 $i1 变量是字符串变量值的长度 -1($length1-1).
  • 现在我们从 WHILE 循环开始,使用条件“$i1>=0”,然后我们将从后面打印字符串的索引值,因为 $i1 值是原始字符串索引值的最后一个。之后,将进行递减以反转原始字符串($i1=$i1-1)。

代码:

<?php
$string1 = "PAVANSAKEkumar";
$length1 = strlen($string1);
$i1=$length1-1;
while($i1>=0){
echo $string1[$i1];
$i1=$i1-1;
}
?>

输出:

PHP 中的反转字符串

4.使用 do while 循环

  • 下面列出了 PHP 中使用 DO while 循环反转字符串的程序。在下面的例子中就像 WHILE LOOP 一样。每个逻辑项都与 WHILE LOOP 相同,但 follow 有点不同,它会先打印输出,而不是首先检查条件。
  • 因此,即使您打印输出,即使条件结果为 FALSE。

代码:

<?php
$string1 = "PAVANSAKEkumaran";
$length1 = strlen($string1);
$i1=$length1-1;
do{
echo $string1[$i1];
$i1=$i1-1;
}
while($i1>=0)
?>

输出:

PHP 中的反转字符串

5. Using the Recursion Technique and the substr()

  • Here in the below example reversing the string is done using the recursion technique and the substr() function. Substr() function will help in getting the original string’s substring. Here a new function called Reverse() also defined which is passed as an argument with the string.
  • At each and every recursive call, substr() method is used in order to extract the argument’s string of the first character and it is called as Reverse () function again just bypassing the argument’s remaining part and then the first character concatenated at the string’s end from the current call. Check the below to know more.
  • The reverse () function is created to reverse a string using some code in it with the recursion technique. $len1 is created to store the length of the string specified. Then if the length of the variable equals to 1 i.e. one letter then it will return the same.
  • If the string is not one letter then the else condition works by calling the letters from behind one by one using “length – -“ and also calling the function back in order to make a recursive loop using the function (calling the function in function using the library function of PHP). $str1 variable is storing the original string as a variable. Now printing the function result which is the reverse of the string.

Code:

<?php
function Reverse($str1){
$len1 = strlen($str1);
if($len1 == 1){
return $str1;
}
else{
$len1--;
return Reverse(substr($str1,1, $len1))
. substr($str1, 0, 1);
}
}
$str1 = "PavanKumarSake";
print_r(Reverse($str1));
?>

Output:

PHP 中的反转字符串

6. Reversing String without using any library functions of PHP

  • The below syntax/ program example is done by swapping the index’s characters using the for loop like first character’s index is replaced by the last character’s index then the second character is replaced by the second from the last and so on until we reach the middle index of the string.
  • The below example is done without using any of the library functions. Here in the below example, $i2 variable is assigned with the length of the original string-1 and $j2 value is stored with value as “0” then in the loop will continue by swapping the indexes by checking the condition “$j2

Code:


Output:

PHP 中的反转字符串

Conclusion

I hope you understood the concept of logic of reversing the input string and how to reverse a string using various techniques using an example for each one with an explanation.

以上是PHP 中的反转字符串的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Factorial in PHP下一篇:Square Root in PHP