当前位置:Gxlcms > PHP教程 > php字符串按照单词进行反转的方法_php技巧

php字符串按照单词进行反转的方法_php技巧

时间:2021-07-01 10:21:17 帮助过:17人阅读

本文实例讲述了php字符串按照单词进行反转的方法。分享给大家供大家参考。具体分析如下:

下面的php代码可以将字符串按照单词进行反转输出,实际上市现将字符串按照空格分隔到数组,然后对数组进行反转输出

  1. <?php
  2. $s = "Reversing a string by word";
  3. // break the string up into words
  4. $words = explode(' ',$s);
  5. // reverse the array of words
  6. $words = array_reverse($words);
  7. // rebuild the string
  8. $s = implode(' ',$words);
  9. print $s;
  10. ?>

输出结果如下:
word by string a Reversing

希望本文所述对大家的php程序设计有所帮助。

人气教程排行