參數(shù) | 描述 |
pattern | 正則表達式 |
subject | 需要匹配檢索的對象 |
matches | 可選,存儲匹配結果的數(shù)組 |
實例:
此實例匹配大寫字母后面帶有.和空格的字符串,只能匹配到J. ,因為preg_match() 匹配成功一次后就會停止匹配,后面不會再匹配了。
?php $str="Daniel J. Gross Catholic High School A. is a faith and family based community committed to developing Christian leaders through educational excellence in the Marianist tradition."; if(preg_match("/[A-Z]. /",$str,$matches)){ print_r($matches); } ?>
輸出結果:
Array ( [0] => J. )
下面給大家介紹preg_match字符串長度問題
preg_match正則提取目標內容,死活有問題,代碼測得死去活來。
后來懷疑PHP 的preg_match有字符串長度限制,果然,發(fā)現(xiàn)“pcre.backtrack_limit ”的值默認只設了100000。
解決辦法:
ini_set('pcre.backtrack_limit', 999999999);
注:這個參數(shù)在php 5.2.0版本之后可用。
另外說說關于:pcre.recursion_limit
pcre.recursion_limit是PCRE的遞歸限制,這個項如果設很大的值,會消耗所有進程的可用堆棧,最后導致PHP崩潰。
也可以通過修改配置來限制:
ini_set('pcre.recursion_limit', 99999);
實際項目應用中,最好也對內存進行限定設置:ini_set('memory_limit', '64M'); , 這樣就比較穩(wěn)妥妥嘎。