PHP preg_match正则表达式的使用_PHP教程
函数用法
int preg_match_all ( string pattern, string subject, array matches [, int flags] )
例1
代码如下 | 复制代码 |
preg_match_all ("|]+>(.*)]+>|U","example: this is a test ",$out, PREG_SET_ORDER);print $out[0][0].", ".$out[0][1]."n"; print $out[1][0].", ".$out[1][1]."n"; ?>
本例将输出:
|
例2
URL 中取出域名
代码如下 | 复制代码 |
// 从 URL 中取得主机名 本例将输出: domain name is: PPP.NET |
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'); , 这样就比较稳妥妥嘎。
推荐阅读
-
能得到你是从什么页面过来的,referer的用处_PHP教程
-
PHP preg_match正则表达式的使用_PHP教程
-
PHP使用GETDATE获取当前日期时间作为一个关联数组的方法
-
为什么小弟我的COOKIE这样无法使用?还有PHP一定要以这种方式编写的吗
-
把1316这个数表示成两个数的和,其中一个为13的倍数,另一个是11的倍数,求这两个数。_PHP教程
-
Thinkphp的list_to_tree 实现无限级分类列出所有节点_PHP教程
-
[视频教程]LAMP兄弟连视PHP函数-preg_match
-
php验证码样式的使用实例分享
-
十款最出色的PHP安全开发库中文详细介绍(1)_PHP教程
-
php foreach 使用&(与运算符)引用赋值要注意的问题_php技巧