Inversion methods: 1. use "explode(' ',$str)" statement to convert string to array; 2. use "array_reverse($arr)" statement to invert the array and return the array in the reverse order of elements; 3. use "implode(' ',$arr)" statement to convert the inverse array to string.
php to invert strings by word
Implementation method:
-
First convert the string into an array of elements by word
-
Then use array_reverse() to invert the array
-
Then convert the inverse array to a string
Implementation code:
1
2
3
4
5
6
7
8
9
10
|
<? php
$ str = "Reversing a string by word" ;
// break the string up into words
$ arr = explode (' ',$str );
// reverse the array of words
$ arr = array_reverse ($arr);
// rebuild the string
$ str = implode (' ',$arr);
print $str;
?>
|
Output results:
-
The explode() function splits a string based on the string separator, i.e. it splits a string into several substrings based on the separator, and then combines these substrings into an array and returns it.
-
The array_reverse() function returns an array in the reverse order of its elements.
-
The implode() function converts a one-dimensional array into a string
Related articles
In php, you can use the max() function to get the maximum value among several numbers. The function works by calculating and returning the maximum value among several specified values, with the syntax "max(value1,value2,...) ;".
php image to base64 method: 1, get an image address; 2, through the "chunk_split(base64_encode($gambar));" method to convert the image to base64 string format can be.
How to merge arrays in PHP? To do this we need to use the array_merge function and the array_merge_recursive function. Here we will introduce these two functions separately.
php array only take the first two elements of the method: 1, use the array_splice () function, syntax "array_splice($arr,0,2)"; 2, use the array_slice () function, syntax "array_slice($arr,0,2)".
String is an important data type in PHP, which how to compare strings is also very common in our development work, there are many kinds of comparison methods to compare strings, here we will introduce the more commonly used several comparison methods.
Difference: 1. Jump is the current URL request success, re-request a new URL; while redirect is the current URL is invalid, was redirected to a new URL. 2. In the jump, the browser will record the current URL and the new URL to the history; while redirect
php merge different array elements method: 1, create a PHP sample file; 2, by "array_keys(array_flip($a) + array_flip($b) + array_flip($c));" way to multiple arrays can be merged and de-weighted.
The solution for the php exec function not taking effect: 1. Open the /etc/php.ini file, delete the exec, then save and restart php-fpm; 2. Change the value of "safe_mode" to off.
The difference between ado and php: 1, ADO is a Microsoft technology, is a programming interface to access data in the database, while PHP is a general open source scripting language;
How to set the local time in php: 1. Use "date_default_timezone_set()" in the header to set the default time zone; 2. Set "date.timezone" in php.ini " in php.ini to set the value of "date.timezone" to PRC.