php字符串按照單詞進(jìn)行反轉(zhuǎn)的方法

字號(hào):


    本文實(shí)例講述了php字符串按照單詞進(jìn)行反轉(zhuǎn)的方法。分享給大家供大家參考。具體分析如下:
    下面的php代碼可以將字符串按照單詞進(jìn)行反轉(zhuǎn)輸出,實(shí)際上市現(xiàn)將字符串按照空格分隔到數(shù)組,然后對(duì)數(shù)組進(jìn)行反轉(zhuǎn)輸出
    <?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;
    ?>
    輸出結(jié)果如下:
    word by string a Reversing
    希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。