php不包含某字符串的正则表达式
摘要: 用正则实现包含某个字符串很容易,但如果实现不包含某个字符串呢?作者给出了一个解决方案。
判断一个字符串中是否含有另一字符串,php有很多方法,如下:
1. 常见函数
strstr($str, "abc");
strstr($str, "abc");
2. 正则匹配
preg_match("/(abc)/is", $str);
preg_match("/(abc)/is", $str);
但是要匹配一个字符串中,不包含某字符串,用正则就比较麻烦了。
1. 如果不用正则如下就可以解决问题
!strstr($str, "abc");
!strstr($str, "abc");
2. 但是用正则呢,就只有这样了
preg_match("/^((?!abc).)*$/is", $str);
preg_match("/^((?!abc).)*$/is", $str);
完整代码示例
$str = "dfadfadf765577abc55fd";
$pattern_url = "/^((?!abc).)*$/is";
if (preg_match($pattern_url, $str))
{
echo "不含有abc!";
}
else
{
echo "含有abc!";
}
$str = "dfadfadf765577abc55fd"; $pattern_url = "/^((?!abc).)*$/is"; if (preg_match($pattern_url, $str)) { echo "不含有abc!"; } else { echo "含有abc!"; }
结果为:false,含有abc!
同时匹配,包含字符串 “abc”,而且不包含字符串 “xyz”的正则表达式:
preg_match("/(abc)[^((?!xyz).)*$]/is", $str);
相关文档:
Google launched their Google App Engine (GAE) a year ago. The free hosting in App Engine is allocated 500 MB of persistent storage and enough CPU and bandwidth for about 5 million page views a month. Also, if you really want more you can see pricing plans.
GAE will support Java going forw ......
[PHP]
;;;;;;;;;;;;;;;;;;;
; About php.ini ;
;;;;;;;;;;;;;;;;;;;
; PHP's initialization file, generally called php.ini, is responsible for
; configuring many of the aspects of PHP's behavior.
; PHP attempts to find and load this configuration from a number of locations.
; The follo ......
以前一直搞C++进行C/S开发,曾经的想法是如果有机会,学学搞网站和嵌入式开发
没想到找到工作的第2天就有了做网站的机会,也赚到了自己大学期间最高的一份工资,10天1000块
在什么的都不会的情况下,尽然答应人事处的老师一个星期写好网站的后台
我承认找到工作后自己是有点狂了,很多事没注意到,很多事后悔不了。。。
......
Php Variable Circular References
Circular-references has been a long outstanding issue with PHP. They are
caused by the fact that PHP uses a reference counted memory allocation
mechanism for its internal variables. This causes problems for longer
running scripts (such as an Applicatio ......
在PHP里,如果你没有手写构造函数,则php在实例化这个对象的时候,会自动为类成员以及类方法进行初始化,分配内存等工作,但是有些时候不能满足我们的要求,比如我们要在对象实例化的时候传递参数,那么就需要手动编写构造函数了,手写构造函数有两种写法,只是表现形式不同,其实本质一样
class test
{
&nb ......