php preg_match正则表达式函数实例讲解
|
preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0 。 preg_match() 匹配成功一次后就会停止匹配,如果要实现全部结果的匹配,则需使用 preg_match_all() 函数。 语法: preg_match (pattern,subject,matches) 参数 描述 pattern 正则表达式 subject 需要匹配检索的对象 matches 可选,存储匹配结果的数组 preg_match()实例: 此实例匹配大写字母后面带有.和空格的字符串,只能匹配到J. ,因为preg_match() 匹配成功一次后就会停止匹配,后面不会再匹配了。 $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有字符串长度限制,果然,发现“pcre.backtrack_limit ”的值默认只设了100000。 解决办法: ini_set('pcre.backtrack_limit',999999999); 注:这个参数在php 5.2.0版本之后可用。 另外说说关于:pcre.recursion_limit pcre.recursion_limit是PCRE的递归限制,这个项如果设很大的值,会消耗所有进程的可用堆栈,最后导致PHP崩溃。 也可以通过修改配置来限制: ini_set('pcre.recursion_limit',99999); 实际项目应用中,最好也对内存进行限定设置:ini_set('memory_limit','64M');,这样就比较稳妥妥嘎。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
