简体   繁体   中英

PHP Regular Expression not matching

I have a really simple PHP regular expression that isn't returning as a match, as seen below.

if(preg_match('[0-9]{3}','123')){
    echo "match";
}
else{
    echo "nope";
}

This should check if the string '123' matches the pattern '[0-9]{3}', which of course it does - however it's not completing the true condition and is instead echoing "nope".

I've also tried:

if(preg_match('[0-9]{3}' '123') == '1'){
    echo "match";
}
else{
    echo "nope";
}

Any ideas? Do I need to configure my server or enable a regex property / library or something?

您忘记添加php分隔符

if(preg_match('~[0-9]{3}~','123')){

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