i have a string .
我有一个字符串。
i want to reverse the letters in every word not reverse the words order.
我想颠倒每个单词中的字母而不是颠倒单词顺序。
like - 'my string'
喜欢 - '我的字符串'
should be
'ym gnirts'
5 个解决方案
#1
This should work:
这应该工作:
$words = explode(' ', $string);
$words = array_map('strrev', $words);
echo implode(' ', $words);
Or as a one-liner:
或者作为一个单行:
echo implode(' ', array_map('strrev', explode(' ', $string)));
#2
echo implode(' ', array_reverse(explode(' ', strrev('my string'))));
This is considerably faster than reversing every string of the array after exploding the original string.
这比在爆炸原始字符串后反转数组的每个字符串要快得多。
#3
Functionified:
<?php
function flipit($string){
return implode(' ',array_map('strrev',explode(' ',$string)));
}
echo flipit('my string'); //ym gnirts
?>
#4
This should do the trick:
这应该做的伎俩:
function reverse_words($input) {
$rev_words = [];
$words = split(" ", $input);
foreach($words as $word) {
$rev_words[] = strrev($word);
}
return join(" ", $rev_words);
}
#5
I would do:
我会做:
$string = "my string";
$reverse_string = "";
// Get each word
$words = explode(' ', $string);
foreach($words as $word)
{
// Reverse the word, add a space
$reverse_string .= strrev($word) . ' ';
}
// remove the last inserted space
$reverse_string = substr($reverse_string, 0, strlen($reverse_string) - 1);
echo $reverse_string;
// result: ym gnirts
#1
This should work:
这应该工作:
$words = explode(' ', $string);
$words = array_map('strrev', $words);
echo implode(' ', $words);
Or as a one-liner:
或者作为一个单行:
echo implode(' ', array_map('strrev', explode(' ', $string)));
#2
echo implode(' ', array_reverse(explode(' ', strrev('my string'))));
This is considerably faster than reversing every string of the array after exploding the original string.
这比在爆炸原始字符串后反转数组的每个字符串要快得多。
#3
Functionified:
<?php
function flipit($string){
return implode(' ',array_map('strrev',explode(' ',$string)));
}
echo flipit('my string'); //ym gnirts
?>
#4
This should do the trick:
这应该做的伎俩:
function reverse_words($input) {
$rev_words = [];
$words = split(" ", $input);
foreach($words as $word) {
$rev_words[] = strrev($word);
}
return join(" ", $rev_words);
}
#5
I would do:
我会做:
$string = "my string";
$reverse_string = "";
// Get each word
$words = explode(' ', $string);
foreach($words as $word)
{
// Reverse the word, add a space
$reverse_string .= strrev($word) . ' ';
}
// remove the last inserted space
$reverse_string = substr($reverse_string, 0, strlen($reverse_string) - 1);
echo $reverse_string;
// result: ym gnirts