例子
"This is an example!" ==> "sihT si na !elpmaxe"
"double spaces" ==> "elbuod secaps"
代碼
function reverseWords($str) {
// Go for it
$str_new = explode(' ',$str);
$str_new = array_map("strrev",$str_new);
$new_str = implode(' ',$str_new);
return $new_str;
}
解析
explode: 用空格拆分字符串為一個(gè)數(shù)組
array_map: 對(duì)數(shù)組中的每個(gè)鍵值執(zhí)行回調(diào)方法,這里是strrev文章來源:http://www.zghlxwxcb.cn/news/detail-734039.html
strrev: 反轉(zhuǎn)字符串,即abc? -> cba文章來源地址http://www.zghlxwxcb.cn/news/detail-734039.html
到了這里,關(guān)于PHP反轉(zhuǎn)字符串中的每個(gè)單詞的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!