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(); //正...