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);
好了,今天的教程就先到这里,有什么问题大家可以留言,我们来讨论下