PHP中的Regex函數preg__PHP教程

來源:互聯網
上載者:User
preg_match(); //用於Regex的匹配,且只匹配一次 preg_match_all();//用於Regex的匹配,會對所有符合規則的都進行匹配 preg_replace(); //Regex替換函數preg_splite(); //正則分割函數 preg_match ( mode, string subject , arraymatches ) 其中mode是正則規則,string subject是要匹配的字串,arraymatches是匹配的結果數組 例子: [php] 輸出:[html] Array ( [0] => 9 ) preg_match_all ( mode, string subject , arraymatches )該函數和preg_match_all()函數的功能類似,不過該函數會匹配所有符合要求的內容,並將之存放到字串中。 例子: [php] 輸出: [html] Array ( [0] => Array ( [0] => 8 [1] => 8 [2] => 9 [3] => 9 [4] => 9 ) ) preg_replace ( mixed pattern, mixed replacement,mixed subject [, int limit] )通過Regex來替換相關內容,類似之前學過的str_replace字串替換,但功能要強於它。 特點:1、替換內容可以是一個正則也可以是數組正則 2、替換內容可以通過修正符e來解決替換執行內容 用途:替換一些比較複雜的內容上,也可以用於內容的轉換上例子1——數組正則: [php] 作者:{author}
地址:{url}"; echo ""; if($tag=preg_replace($mode,$re,$str)){ echo $tag; }else{ echo "替換不成功!"; } ?> 輸出: [html] 標題:code cloud 作者:qianshou 地址:http://codecloud.duapp.com/ 例子2——ubb代碼的替換:[php] 輸出:[html 歡迎來到我的部落格:http://blog.csdn.net/qsyzb preg_split ( string pattern, string subject [, intlimit [, int flags]] )通過Regex來切割相關內容,類似之前學過的explode切割函數,但explode只能按照一種方式切割有局限性。 例子: [php] 輸出:[html] Array ( [0] => one [1] => two [2] => three [3] => four )

http://www.bkjia.com/PHPjc/477144.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477144.htmlTechArticlepreg_match(); //用於Regex的匹配,且只匹配一次 preg_match_all();//用於Regex的匹配,會對所有符合規則的都進行匹配 preg_replace(); //正...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.