本文實例講述了php字符串按照單詞進行反轉的方法。分享給大家供大家參考。具體分析如下:
下面的php代碼可以將字符串按照單詞進行反轉輸出,實際上市現將字符串按照空格分隔到數組,然后對數組進行反轉輸出
<?php
$s = "Reversing a string by word";
// break the string up into words
$words = explode(' ',$s);
// reverse the array of words
$words = array_reverse($words);
// rebuild the string
$s = implode(' ',$words);
print $s;
?>
輸出結果如下:
word by string a Reversing
希望本文所述對大家的php程序設計有所幫助。
更多信息請查看IT技術專欄
2026國考·省考課程試聽報名