切換
舊版
前往
大廳
主題

PHP #2 字串處理函式

CW | 2020-06-03 18:23:55 | 巴幣 2 | 人氣 215

字串處理函式
(建議以電腦閱讀此篇)

取得長度
    $string = "Hello word";
    echo strlen($string);
    // 輸出結果:10
    $string = "哈囉文字";
    echo strlen($string);
    // 輸出結果:12

搜尋出現位置
    $string = "Hello word! by. php";
    echo strpos($string, "word");
    // 位置從 0 開始計算

取代替換
    $string = "Hello word! by. php";
    echo str_replace("word", "Tom", $string);
    // 輸出: Hello Tom! by. php

部分擷取
    $string = "Hello word! by. php";
    echo substr($string, 6, 9);
    // 輸出: word! by.
    // substr(目標字串, 開始位置, 擷取長度)




如果有錯誤,歡迎下方留言討論喔 ~
送禮物贊助創作者 !
0
留言

創作回應

相關創作

更多創作