题
我有一个字符串。
我想扭转中的字母的每一个字不能逆转的话顺序。
像 - '我的字符串'
应
'YM gnirts'
解决方案
此应该工作:
$words = explode(' ', $string);
$words = array_map('strrev', $words);
echo implode(' ', $words);
或者作为一衬垫:
echo implode(' ', array_map('strrev', explode(' ', $string)));
其他提示
echo implode(' ', array_reverse(explode(' ', strrev('my string'))));
这比爆炸原始字符串后反转所述阵列的每个字符串明显更快。
Functionified:
<?php
function flipit($string){
return implode(' ',array_map('strrev',explode(' ',$string)));
}
echo flipit('my string'); //ym gnirts
?>
此应达到目的:
function reverse_words($input) {
$rev_words = [];
$words = split(" ", $input);
foreach($words as $word) {
$rev_words[] = strrev($word);
}
return join(" ", $rev_words);
}
我会做:
$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
不隶属于 StackOverflow