- PHP從入門到精通(第4版)(軟件開發(fā)視頻大講堂)
- 明日科技
- 1274字
- 2020-11-28 17:36:36
6.3 PCRE兼容正則表達(dá)式函數(shù)
視頻講解:光盤\TM\lx\6\03 PCRE兼容正則表達(dá)式函數(shù).mp4
PHP中提供了兩套支持正則表達(dá)式的函數(shù)庫,但是由于PCRE函數(shù)庫在執(zhí)行效率上要略優(yōu)于POSIX函數(shù)庫,所以這里只講解PCRE函數(shù)庫中的函數(shù)。實現(xiàn)PCRE風(fēng)格的正則表達(dá)式的函數(shù)有7個,下面就來了解一下這7個PCRE函數(shù)。
6.3.1 preg_grep()函數(shù)
函數(shù)語法:
array preg_grep ( string pattern, array input )
函數(shù)功能:使用數(shù)組input中的元素一一匹配表達(dá)式pattern,最后返回由所有相匹配的元素所組成的數(shù)組。
【例6.1】在數(shù)組$arr中匹配具有正確格式的電話號(010-1234****等),并保存到另一個數(shù)組中。實例代碼如下:(實例位置:光盤\TM\sl\6\1)
<?php $preg='/\d{3,4}-? \d{7,8}/'; //國內(nèi)電話格式表達(dá)式 $arr=array('043212345678', '0431-7654321', '12345678'); //包含元素的數(shù)組 $preg_arr=preg_grep($preg, $arr); //使用preg_grep()查找匹配元素 var_dump($preg_arr); //查看新數(shù)組結(jié)構(gòu) ?>
運行結(jié)果如圖6.1所示。

圖6.1 preg_grep()函數(shù)
6.3.2 preg_match()和preg_match_all()函數(shù)
函數(shù)語法:
int preg_match/preg_match_all ( string pattern, string subject [, array matches] )
函數(shù)功能:在字符串subject中匹配表達(dá)式pattern。函數(shù)返回匹配的次數(shù)。如果有數(shù)組matches,那么每次匹配的結(jié)果都將被存儲到數(shù)組matches中。
函數(shù)preg_match()的返回值是0或1。因為該函數(shù)在匹配成功后就停止繼續(xù)查找了。而preg_match_all()函數(shù)則會一直匹配到最后才會停止。參數(shù)array matches對于preg_match_all()函數(shù)是必須有的,而對前者則可以省略。
【例6.2】使用preg_match()函數(shù)和preg_match_all()函數(shù)來匹配字串$str,并返回各自的匹配次數(shù)。實例代碼如下:(實例位置:光盤\TM\sl\6\2)
<?php $str = 'This is an example! '; $preg = '/\b\w{2}\b/'; $num1 = preg_match($preg, $str, $str1); echo $num1.'<br>'; var_dump($str1); $num2 = preg_match_all($preg, $str, $str2); echo '<p>'.$num2.'<br>'; var_dump($str2); ?>
運行結(jié)果如圖6.2所示。

圖6.2 preg_match()和preg_match_all()函數(shù)
6.3.3 preg_quote()函數(shù)
函數(shù)語法:
string preg_quote ( string str [, string delimiter] )
函數(shù)功能:該函數(shù)將字符串str中的所有特殊字符進(jìn)行自動轉(zhuǎn)義。如果有參數(shù)delimiter,那么該參數(shù)所包含的字串也將被轉(zhuǎn)義。函數(shù)返回轉(zhuǎn)義后的字串。
【例6.3】輸出常用的特殊字符,并且將字母b也當(dāng)作特殊字符輸出。實例代碼如下:(實例位置:光盤\TM\sl\6\3)
<?php $str = '! 、$、^、*、+、.、[、]、\\、/、b、<、>'; $str2 = 'b'; $match_one = preg_quote($str, $str2); echo $match_one; ?>
結(jié)果為:\! 、\$、\^、\*、\+、\.、\[、\]、\\、/、\b、\<、\>
注意
這里的特殊字符是指正則表達(dá)式中具有一定意義的元字符,其他如“@”“#”等則不會被當(dāng)作特殊字符處理。
6.3.4 preg_replace()函數(shù)
函數(shù)語法:
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
函數(shù)功能:該函數(shù)在字符串subject中匹配表達(dá)式pattern,并將匹配項替換成字串replacement。如果有參數(shù)limit,則替換limit次。
說明
如果參數(shù)中調(diào)用的是數(shù)組,有可能在調(diào)用過程中并不是按照數(shù)組的key值進(jìn)行替換,所以在調(diào)用之前需要使用ksort()函數(shù)對數(shù)組重新排列。
【例6.4】本例實現(xiàn)一個常見的UBB代碼轉(zhuǎn)換功能,將輸入的“[b]…[/b]”“[i]…[/i]”等類似的格式轉(zhuǎn)換為HTML能識別的標(biāo)簽。實例代碼如下:(實例位置:光盤\TM\sl\6\4)
<?php $string = '[b]粗體字[/b]'; $b_rst = preg_replace('/\[b\](.*)\[\/b\]/i', '<b>$1</b>', $string); echo $b_rst; ?>
結(jié)果為:粗體字
說明
preg_replace()函數(shù)中的字串“$1”是在正則表達(dá)式外調(diào)用分組,按照$1、$2排列,依次表示從左到右的分組順序,也就是括號順序。$0表示的是整個正則表達(dá)式的匹配值。關(guān)于反向引用的其他用法,請參考6.2.12節(jié)。
6.3.5 preg_replace_callback()函數(shù)
函數(shù)語法:
mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )
preg_replace_callback()函數(shù)與preg_replace()函數(shù)的功能相同,都用于查找和替換字串。不同的是,preg_replace_callback()函數(shù)使用一個回調(diào)函數(shù)(callback)來代替replacement參數(shù)。
注意
在preg_replace_callback()函數(shù)的回調(diào)函數(shù)中,字符串使用“''”,這樣可以保證字符串中的特殊符號不被轉(zhuǎn)義。
【例6.5】本例使用回調(diào)函數(shù)來實現(xiàn)UBB功能。實例代碼如下:(實例位置:光盤\TM\sl\6\5)
<?php function c_back($str){ $str = "<font color=$str[1]>$str[2]</font>"; return $str; } $string = '[color=blue]字體顏色[/color]'; echo preg_replace_callback('/\[color=(.*)\](.*)\[\/color\]/U', "c_back", $string); ?>
結(jié)果為:字體顏色
注意
本例運行結(jié)果“字體顏色”為藍(lán)色字體,書中看不出效果,請運行本書光盤附帶的實例。
6.3.6 preg_split()函數(shù)
函數(shù)語法:
array preg_split ( string pattern, string subject [, int limit ] )
函數(shù)功能:使用表達(dá)式pattern來分割字符串subject。如果有參數(shù)limit,那么數(shù)組最多有l(wèi)imit個元素。該函數(shù)與ereg_split()函數(shù)的使用方法相同,這里不再舉例。
- Java Web開發(fā)學(xué)習(xí)手冊
- JavaScript從入門到精通(微視頻精編版)
- JSP網(wǎng)絡(luò)編程(學(xué)習(xí)筆記)
- Modular Programming with Python
- Mastering RabbitMQ
- CentOS 7 Server Deployment Cookbook
- Learning AWS Lumberyard Game Development
- Java程序設(shè)計與計算思維
- jQuery開發(fā)基礎(chǔ)教程
- RabbitMQ Essentials
- Visual FoxPro 6.0程序設(shè)計
- Exploring SE for Android
- Visual Basic程序設(shè)計實驗指導(dǎo)及考試指南
- Raspberry Pi Robotic Projects
- Analytics for the Internet of Things(IoT)