[英]Is there any function to compare a string with two regular expressions at a time in php
I found preg_match
and preg_match_all
but these will work with only one regular expression at a time. 我找到了
preg_match
和preg_match_all
但它们一次只能使用一个正则表达式。
function match(){
$pattern = array(
'/^\-?\+?[0-9e1-9]+$/',
'/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9 ]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/'
);
$string = '234';
$pattern_mod = import(",",$pattern);
preg_match($pattern_mod ,$string);
this is what i want to do 这就是我想做的
You could use regex lookahead in which a subject will first try to match foo and them try to match bar. 您可以使用正则表达式前瞻,其中主题将首先尝试匹配foo,然后他们尝试匹配bar。 Like so:
像这样:
$regexPass='/^(?=((.*[A-Za-z].*[0-9].*)|(.*[0-9].*[A-Za-z].*)))(.{6,})$/';
The regex above says that it must match the first expression ((.*[A-Za-z].*[0-9].*)|(.*[0-9].*[A-Za-z].*)))
in this case alphanumerical with at least one number and one leter, AND THEM matching at least 6 digits. 上面的正则表达式表示它必须与第一个表达式
((.*[A-Za-z].*[0-9].*)|(.*[0-9].*[A-Za-z].*)))
在这种情况下为字母数字形式,至少包含一个数字和一个字母,并且ANDM至少匹配6位数字。
In a simpler way you could match foo and them have an on the end 用一种更简单的方式,您可以匹配foo,并且它们的末尾有一个
$regexPass='/^(?=.\*foo.\*)(.\*n)$/';
If I've "decrypted" correctly your question, I suppose that you have simply to use and
(if have to match both), or
(if have to match at least one) operators with preg_match
or preg_match_all
php function. 如果我正确地“解密”了您的问题,我想您只需使用
preg_match
或preg_match_all
php函数使用and
(如果必须同时匹配) or
(如果必须至少匹配一个)运算符。
This is programming baby :) 这是编程宝贝:)
Like this 像这样
$string='myString';
if( (preg_match(pattern,$string) and (preg_match(otherPattern,$string) )
{
//do things
[...]
}
or
if( (preg_match(pattern,$string) or (preg_match(otherPattern,$string) )
{
//do things
[...]
}
Where pattern
and otherPattern
are your regexp patterns 其中
pattern
和otherPattern
是您的正则表达式模式
I have two option, you can use any one fits your need (i am using and condition for just an example)- 我有两种选择,您可以使用任何一种适合您的需求(我仅以and条件为例)-
1) 1)
$subject = "abcdef";<br />
$pattern = '/^def/'; <br />
$result = preg_match($pattern, substr($subject,3)); <br/>
$result1 = preg_match("/php/i", "PHP is the web scripting language of choice."); <br />
echo ($result && $result1)?"true" :"false"
2) 2)
echo (preg_match($pattern, substr($subject,3)) && preg_match("/php/i", "PHP is the web scripting language of choice."))?"true" :"false";
Though both are almost same lines but the way of code is different, choose one that fits your taste. 尽管两者几乎都是同一行,但是代码的方式却不同,请选择适合您的口味。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.