這篇文章主要介紹了php字符串按照單詞進行反轉的方法,涉及php中array_reverse、explode及implode等函數的使用技巧,需要的朋友可以參考下
本文實例講述了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程序設計有所幫助。
新聞熱點
疑難解答