php字串的替換,分割和串連方法,php字串替換分割_PHP教程

來源:互聯網
上載者:User

php字串的替換,分割和串連方法,php字串替換分割


本文執行個體講述了php字串的替換,分割和串連方法。分享給大家供大家參考,具體如下:

字串的替換

1. 執行一個Regex的搜尋和替換
複製代碼 代碼如下:mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

搜尋subject中匹配pattern的部分, 以replacement進行替換.

2. 子字串替換
複製代碼 代碼如下:mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
該函數返回一個字串或者數組。該字串或數組是將 subject 中全部的 search 都被 replace 替換之後的結果。

字串的分割和串連

通過一個Regex分隔字串

說明

1. array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

通過一個Regex分隔給定字串.

2. explode — 使用一個字串分割另一個字串

說明:

array explode ( string $separator , string $string [, int $limit ] )

$str = 'one|two|three|four';// 正數的 limitprint_r(explode('|', $str, 2));// 負數的 limit(自 PHP 5.1 起)print_r(explode('|', $str, -1));

以上常式會輸出:

Array(  [0] => one  [1] => two|three|four)Array(  [0] => one  [1] => two  [2] => three)

3. string implode(string glue, array pieces) ———— 串連數組稱為字串

$lan=array("a","b","c");implode("+", $lan);//a+b+c

更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP數組(Array)操作技巧大全》、《PHP資料結構與演算法教程》、《PHP數學運算技巧總結》、《php日期與時間用法總結》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

http://www.bkjia.com/PHPjc/1133093.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133093.htmlTechArticlephp字串的替換,分割和串連方法,php字串替換分割 本文執行個體講述了php字串的替換,分割和串連方法。分享給大家供大家參考,具體如...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.