=0;$i--){$r.=substr($str,$i,1);}"; 2. Directly use strrev( ) function to reverse a string, syntax "strrev($str)"."/> =0;$i--){$r.=substr($str,$i,1);}"; 2. Directly use strrev( ) function to reverse a string, syntax "strrev($str)".">

Home  >  Article  >  Backend Development  >  How to reverse string in php

How to reverse string in php

青灯夜游
青灯夜游Original
2022-07-12 20:34:192922browse

Two methods to achieve string reversal: 1. Use the for statement to traverse the string in reverse order, and use the substr() function in the loop body to intercept characters one by one from back to front, and splice them into a new string. , syntax "$r='';for($i=string length-1;$i>=0;$i--){$r.=substr($str,$i,1);}"; 2. Directly use the strrev() function to reverse the string, the syntax is "strrev($str)".

How to reverse string in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php implements strings Two methods of reversal

Method 1: Using the for statement and substr() function

  • Use the for statement Traverse the string in reverse order

  • In the loop body, use the substr() function to intercept characters one by one from back to front and splice them into a new string

Implementation code:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="123456789";
echo "字符串反转前:<br>";
var_dump($str);

$result = &#39;&#39;;
$len = strlen($str);//获取字符串长度
for ($i = $len-1; $i>=0; $i--){
	$result.= substr($str,$i,1);
}
echo "字符串反转后:<br>";
var_dump($result);
?>

How to reverse string in php

Method 2: Directly use the strrev() function

strrev() is The string reversal function can reverse a string and return the reversed string.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="A123456789B";
echo "字符串反转前:<br>";
var_dump($str);

$result = strrev($str);
echo "字符串反转后:<br>";
var_dump($result);
?>

How to reverse string in php

Explanation: The

substr() function can intercept a certain length of characters from the specified position of the string. This section is The intercepted characters can be called "substrings" or "substrings", and their syntax format is as follows:

substr($string, $start [, $length])

The parameter description is as follows:

  • $string: the string that needs to be intercepted , the string contains at least one character;
  • $start: intercept the starting position of the string;
    • If $start is a non-negative number, then the string will start from the $start of $string Start intercepting at the character, and $start starts counting from 0. For example, in the string "abcdef", the character at position 0 is "a", the character at position 2 is "c", etc.;
    • If $start is a negative number, the string will start from $ The end of string starts at the $start character forward, and $start starts counting from -1. For example, in the string "abcdef", the character at position -1 is "f", the character at position -3 is "d", etc.;
    • If the length of $string is less than $start, it will be returned FALSE.
  • $length: Optional parameter, indicating the length of the intercepted string.
    • If $length is a positive number, then the string will be truncated up to $length characters from the $start position;
    • If $length is a negative number, then $length characters from the end of $string Characters will be omitted (if $start is a negative number, it will be counted from the end of the string);
    • If the value of $length is 0, FALSE or NULL, an empty string will be returned;
    • If $length is not provided, the returned substring will start from the $start position until the end of the string.

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to reverse string in php. 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