get every words backwards using php
Another way to do it with array_reverse()
,
<?php
$str = 'Hello World';
$strArray = explode(" ", $str);
$strArray = array_reverse($strArray);
$str = implode($strArray, " ");
echo $str;
?>
DEMO: https://3v4l.org/ZfEqQ
Reverse the order of space-separated words in a string
Try this:
$s = 'Hello everybody in stackoverflow';
echo implode(' ', array_reverse(explode(' ', $s)));
Reverse the order of letters in the last word in a string
I have been Found the answer. For my own question
$split = explode(" ", $words);
$v_last = $split[count($split)-1];
$reverse = strrev($v_last);
$f_word = chop($words,$v_last);
echo $f_word;
echo $reverse;
Reverse the direction of all sequences of letters in a string
$word = "word,!pineapple--pizza";
$revd = preg_replace_callback('#([A-Za-z]+)#', 'rev_first', $word);
function rev_first($matches){
return strrev($matches[1]);
}
http://3v4l.org/PXEs8
Is this what you're looking for?
Reverse letters in each word of a string without using native splitting or reversing functions
$string = "I am a boy";
$reversed = "";
$tmp = "";
for($i = 0; $i < strlen($string); $i++) {
if($string[$i] == " ") {
$reversed .= $tmp . " ";
$tmp = "";
continue;
}
$tmp = $string[$i] . $tmp;
}
$reversed .= $tmp;
print $reversed . PHP_EOL;
>> I ma a yob
Reverse a string with php
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];
}
Reverse each word of a string separately
$Str = 'Hello world';
$Words = preg_split('/\s+/', $Str); // or explode(' ', $Str);
foreach ($Words as $Word)
echo strrev($Word) . ' ';
output
olleH dlrow
Related Topics
PHP Loop; How to Print Each Result and Delay It for a Second Before Echoing Another Result
Convert an Array of Strings, Each String Has Dot Separated Values, to a Multidimensional Array
How to Uniquely Identify a Computer
Gmail API: 400 Bad Request When Trying to Send an Email (PHP Code)
Saving a Product Custom Field and Displaying It in Cart Page
How to Implement Exchange Rate in PHP
Download Multiple Files as a Zip Folder Using PHP
Calling PHP Function from Jquery
Do I Have to Use MySQL_Real_Escape_String If I Bind Parameters
How to Make PHP Curl Request with Port Number
Detect How Long It Takes for a File to Upload (Php)
Curl Import Character Encoding Problem
Mysql: Group by Two Columns and Sum
PHP - Syntax Error, Unexpected T_Constant_Encapsed_String
Why Composer Can Not Find My Brand New Package on Packagist.Org
Pass the Chosen Product Variations Data into Contact Form 7 Enquiry Form
Mysqli Join Tables from 2 Different Databases
Eloquent Groupby Make "Sqlstate[42000]" with Valid SQL Query in Laravel 5.3