Reverse a string with php

php reverse words in a string
reverse number in php without using function
palindrome in php
replace string php
write php and html page to get the reverse of entered string in textbox when button is clicked
php reverse array
reverse the string hello world
string functions in php

I am trying to find a way to reverse a string, I've seen alternatives but i wanted to to it this way thinking outside the box and not using anyone else's code as an alternative, the code below reverses the string but I keep getting this error:

Notice: Undefined offset: 25 in C:\wamp\www\test\index.php on line 15

25 being the length of the string which is being deincremented.

//error_reporting(NULL);
$string = trim("This is a reversed string");

//find length of string including whitespace
$len =strlen($string);

//slipt sting into an array
$stringExp = str_split($string);

//deincriment string and echo out in reverse
for ($i = $len; $i >=0;$i--)
{
echo $stringExp[$i];
}

thanks in advance

As others said, there's strrev() to do this.

If you want to build it on your own (for learning?): your problem is that you're starting with your index one too high - a string of length 25 is indexed from 0 to 24, so your loop has to look like this:

for ($i = $len - 1; $i >=0;$i--)
{
   echo $stringExp[$i];
}

strrev - Manual, PHP String Reference. Example. Reverse the string "Hello World!": <?php echo strrev("Hello Definition and Usage. The strrev() function reverses a string. Reverse String in PHP Using Various Loops Various Loops like FOR LOOP, WHILE LOOP, DO WHILE LOOP, RECURSIVE METHOD, etc.. in order to Reverse the String in the PHP Programming Language. 1.Using strrev() function

You're trying much too hard, always consult the manual and/or a search engine to check if there are native functions to do what you want before you end up "reinventing the wheel":

strrev — Reverse a string

http://php.net/manual/en/function.strrev.php

$string = "This is a reversed string";
echo strrev($string);
// Output: gnirts desrever a si sihT

PHP strrev() Function, Reversing string using strrev(): The strrev() function is a built-in function available in PHP and is used to reverse strings. This function takes a string as argument  Return Values. Returns the reversed string. Examples. Example #1 Reversing a string with strrev()

$string = 'mystring';
$length = strlen($string);

for ($i = $length; $i > 0; $i--){
echo $string[$i-1];
}

OUTPUT: gnirtsym

PHP, PHP Function Exercises, Practice and Solution: Create a function to reverse a string. Reverse String. A string can be reversed either using strrev() function or simple PHP code. For example, on reversing JAVATPOINT it will become TNIOPTAVAJ. Logic: Assign the string to a variable. Calculate length of the string. Declare variable to hold reverse string. Run for loop. Concatenate string inside for loop. Display reversed string.

You must get $len-1 because string starts from 0 to $len-1

PHP Function Exercise: Reverse a string, Some time ago during a job interview I got the task to reverse a string in PHP without using strrev . My first solution was something like this: $s = '  PHP Function Exercises, Practice and Solution: Create a function to reverse a string.

There is a function for this strrev

Reverse string without strrev, Answer: Use the PHP strrev() function. You can use the PHP strrev() to reverse the text writing direction of a string. Let's take a look at an example to understand​  I am trying to find a way to reverse a string, I've seen alternatives but i wanted to to it this way thinking outside the box and not using anyone else's code as an alternative, the code below reve

How to Reverse a String in PHP, The implementation below is the most simple, taking advantage of PHP's vast amount of 'built-in' functions to reverse the supplied string. function  To make this happen we have to use some PHP functions. First of all, I’ll convert this string into an array. After that, I’ll reverse the array. Then convert that array into a string. We will get the output. Let’s get into the code. Assign string to a variable. 1. Create a String Variable

Reversing a String in PHP · Edd Mann, Guuide to Reverse String in PHP. Here we discuss the logic and how to reverse a string using various loops with respective examples. Specifies the string to reverse: Technical Details. Return Value: Returns the reversed string: PHP Version: 4+ PHP String Reference. COLOR PICKER. HOW TO. Tabs Dropdowns

Reverse String in PHP, Reversing string using strrev(): The strrev() function is a built-in function available in PHP and is used to reverse strings. This function takes a string as argument  Reversing a string is one of the most basic string operations and is used very frequently by developers and programmers. PHP comes with a built-in function to reverse strings. The strrev() function is a built-in function in PHP and is used to reverse a string. This function does not make any change in the original string passed to it as a parameter.

Comments
  • Your string index starts at 0, so the 25th character's index is 24. Change your code so that $i goes from 24 to 0. As many posters have answered below, you should also look for a function to do what you need in the PHP function libraries.
  • Thanks I stumbled on the 0 being counted as One, and yes there are libraries to do this but I wanted to try and do it differently without them ;-)
  • I added an answer on your problem here, stackoverflow.com/a/30722030/137196. Feel free to adjust as needed. Thanks
  • Thanks that's where I was going wrong forgot that 0 it counted so when deincrimenting the length it was looking for 25 which wasn't there, and yes I know strrev is available but i was trying to do it without relying on pre-built functions. Youve saved me from a headache :-)
  • Be careful when using strrev on a utf8 string! See this answer
  • Thanks Yes strrev is the easy way but I was trying to work it out methodically ie(the long way) its for an interview I have today.
  • Exactly what i was looking for as oezi above mentioned about the there being 24 array keys and not 25, cheers