The "?=" combination means "the next text must be like this". This construct doesn't capture the text.
(1)这个例子可以获取 URL 中的协议部分,比如 https,ftp,注意 ?: 后面的部分不在返回的内容中。
$str = "http://www.google.com"; $str = "https://www.google.com"; $preg = '/[a-z]+(?=:)/'; preg_match($preg,$str,$arr); print_r($arr);
(2)"invisible" 分隔符
也叫 “zero-width” 分隔符,参考下面的例子:
$str = ("chinaWorldHello");
$preg = "/(?=[A-Z])/";
$arr = preg_split($preg,$str);
print_r($arr);
(3)匹配强密码
instead of specifying the order that things should appear, it's saying that it must appear but we're not worried about the order.
The first grouping is (?=.{8,}). This checks if there are at least 8 characters in the string. The next grouping (?=.[0-9]) means "any alphanumeric character can happen zero or more times, then any digit can happen". So this checks if there is at least one number in the string. But since the string isn't captured, that one digit can appear anywhere in the string. The next groupings (?=.[a-z]) and (?=.[A-Z]) are looking for the lower case and upper case letter accordingly anywhere in the string.
$str= "HelloWorld2016";
if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $str,$arr)){
print_r($arr);
}
向后查找 ?<=
?<= 表示假如匹配到特定字符,则返回该字符后面的内容。
?= 表示假如匹配到特定字符,则返回该字符前面的内容。
$str = 'chinadhello'; $preg = '/(?<=a)d(?=h)/'; preg_match($preg, $str, $arr); print_r($arr);
好了,今天的教程就先到这里,有什么问题大家可以留言,我们来讨论下
PHP正在进行时-变量详解及字符串动态插入变量这篇文章主要介绍了PHP正在进行时-变量详解及字符串动态插入变量的方法,非常不错,具有参考借鉴价值,需要的朋友
PHP正则匹配操作简单示例【preg_match_all应用】这篇文章主要介绍了PHP正则匹配操作,结合简单实例形式分析了php中preg_match_all针对HTML标签中P元素及img src元素内容的
PHP正则匹配中英文、数字及下划线的方法【用户名验证】 原创 这篇文章主要介绍了PHP正则匹配中英文、数字及下划线的方法,可用于针对用户名的验证操作,需要的朋友可以参考下
PHP正则删除HTML代码中宽高样式的方法这篇文章主要介绍了PHP正则删除HTML代码中宽高样式的方法,涉及php针对HTML代码的正则匹配、替换等操作技巧,需要的朋
php正则删除img标签的方法示例 原创 这篇文章主要介绍了php正则删除img标签的方法,结合具体实例形式分析了php针对img标签正则匹配相关操作技巧,需要的朋
PHP正则删除html代码中a标签并保留标签内容的方法 原创 这篇文章主要介绍了PHP正则删除html代码中a标签并保留标签内容的方法,涉及php基于正则的字符串匹配与子表达式操作