Reputation: 13
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?
Upvotes: 1
Views: 60
Reputation: 174706
You forget to add the php delimiters.
if(preg_match('~[0-9]{3}~','123')){
Upvotes: 5