發新話題

[分享] 《PHP》『字串函式庫』

《PHP》『字串函式庫』substr_replace 取代字串的一部份字串

substr_replace ---  取代字串的一部份字串

語法 : string substr_replace (string string, string replacement, int start [, int length])

說明 :

substr_replace( )取代 string的部份字串,由參數 start和 length來指定界限,以參數 replacement來取代。

如果參數 start是正數,取代的字串將會開始於 string的第 start個字元。

如果參數 start是正數,取代的字串將會開始於 string結尾的第 start個字元。

如果有給予參數 length而且是正數時,它表示 string要被取代的部份字串的長度。如果 length是負數時,它表示從 string結尾算起,要中止取代的字元的數目。如果沒有給予這個參數,則它會預設成參數 string的長度。

Example :

<?php

    $var = 'ABCDEFGH:/MNRPQR/';

    echo "Original: $var<hr>\n";

    /* These two examples replace all of $var with 'bob'. */

    echo substr_replace ($var, 'bob', 0) . "<br>\n";

    echo substr_replace ($var, 'bob', 0, strlen ($var)) . "<br>\n";

    /* Insert 'bob' right at the beginning of $var. */

    echo substr_replace ($var, 'bob', 0, 0) . "<br>\n";

    /* These next two replace 'MNRPQR' in $var with 'bob'. */

    echo substr_replace ($var, 'bob', 10, -1) . "<br>\n";

    echo substr_replace ($var, 'bob', -7, -1) . "<br>\n";

    /* Delete 'MNRPQR' from $var. */

    echo substr_replace ($var, '', 10, -1) . "<br>\n";

?>

注意 : 此函式是PHP 4.0中新增的函式

參考 : str_replace( )  substr( )


TOP

《PHP》『字串函式庫』trim 去除字串開始處與結束處的空白

trim ---  去除字串開始處與結束處的空白

語法 : string trim (string str)

說明 :

此函式去除字串開始處與結束處的空白,並且傳回去除空白後的字串。它目前會去除的空白字元有 : "\n"、"\r"、"\t"、"\v"、"\0" 和一般的空白。

參考 : chop( )  ltrim( )

TOP

《PHP》『字串函式庫』ucfirst 將字串的第一個字元大寫

ucfirst ---  將字串的第一個字元大寫

語法 : string ucfirst (string str)

說明 :

如果參數 str的第一個字元是字母,則將它轉成大寫的。

注意 : 字母字元是由目前場所來決定的,意思是說,例如在預設的 "C"場所時,像是曲音A就不會被轉換。

Example :

<?php

    $text = 'mary had a little lamb and she loved it so.';

    $text = ucfirst ($text);      // $text is now Mary had a little lamb // and she loved it so.

?>

參考 : strtoupper( )  strtolower( )

TOP

《PHP》『字串函式庫』ucwords 將字串中各個單字的第一個字元大寫

ucwords ---  將字串中各個單字的第一個字元大寫

語法 : string ucwords (string str)

說明 :

如果參數 str中的各個單字的第一個字元是字母時,則將它轉成大寫的。

Example :

<?php

    $text = "mary had a little lamb and she loved it so.";

    $text = ucwords($text);      // $text is now: Mary Had A Little // Lamb And She Loved It So.

?>

參考 : strtoupper( )  strtolower( )  ucfirst( )

TOP

《PHP》『字串函式庫』wordwrap 使用字串斷行字元將字串包裹成指定的字元數目

wordwrap ---  使用字串斷行字元將字串包裹成指定的字元數目

語法 : string wordwrap (string str [, int width [, string break]])

說明 :

每隔 width個字元就將 str包裹起來,使用參數 break將它斷行。

如果沒有給予參數 width和 break,wordwrap( )會自動地每隔75個字元就將 str包裹起來,並且使用斷行 '\n'。

Example :

<?php

    $text = "The quick brown fox jumped over the lazy dog.";

    $newtext = wordwrap( $text, 20 );

    echo "$newtext\n";

?>

此範例將會顯示 :

The quick brown fox

jumped over the lazy

dog.

參考 : nl2br( )

TOP

這一次小弟終於一下子終於搞懂囉.......

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。