在php中,我們知道正則表達式過濾函數有兩個,分別是preg_filter()與preg_replace(),但是不一定所有人都知道他們之間的區(qū)別在哪里。今天小編就帶大家一起來了解一下。

PHP正則替換過濾函數
怎么說呢 正則替換過濾函數 跟先前我們說的替換函數很類似 ,就只有一點點的小區(qū)分 不知道大家發(fā)現沒有!!
==preg_filter()==函數: 執(zhí)行一個正則表達式搜索和替換
通常情況下preg_filter()函數等價于preg_replace()函數
案例1代碼如下:
$arr=array('1.jpg','2.txt','3.doc','4.exe','5.php');
$pattern='/\.jpg|\.txt/';$replacement='';
$result1=preg_replace($pattern, $replacement, $arr);
$result2=preg_filter($pattern, $replacement, $arr);
show($result2);preg_filter()和preg_replace()的實際區(qū)別
案例2代碼如下:
$pattern=array( "/\d+/", "/ccc/" ); $replacement=array( '1024', 'PHP' ); $string=array( '1234aaa', 'abbbccc', 'wampserver' ); $result1=preg_replace($pattern, $replacement, $string); show($result1); $result2=preg_filter($pattern, $replacement, $string); show($result2);
所以區(qū)別如下:
preg_filter()只會返回發(fā)生替換過濾后的數組元素,而沒有替換的數組元素沒有返回
preg_replace() 返回的不僅是發(fā)生替換過濾后的數組元素,并且沒有發(fā)生替換的元素也會保留下來并且返回!
其實大家只要一測試打印 出彼此的結果 就可以馬上知道相互之間的區(qū)別了 !






