简体   繁体   中英

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.

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. 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.

In a simpler way you could match foo and them have an on the end

$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.
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

I have two option, you can use any one fits your need (i am using and condition for just an example)-

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)

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM