phpboy07 Posted June 26, 2013 Share Posted June 26, 2013 <?php $user = "john davy/"; if(preg_match('/\w/', $user)) { echo 'TRUE'; }?> this statement should return false because my string variable has a / character which my if statements only returns true if it is a variable,digit and underscore i think..what is the problem of this statement when i'm running this it always returns true Quote Link to comment https://forums.phpfreaks.com/topic/279592-pcre-expressions/ Share on other sites More sharing options...
kicken Posted June 26, 2013 Share Posted June 26, 2013 You're not checking if the string contains ONLY characters that match \w. You're simply checking that the string contains A SINGLE CHARACTER that matches \w. If you want to limit the entire string to only \w characters, you need to include the start- and end-of-string anchors in the regex: /^\w$/ That still will only allow a single character however. If you want to allow multiple characters, then you need to add some kind of repetition specifier, such as + /^\w+$/ That still won't match your input however, because \w does not match a space character. You'd either have to make your string no-spaces (johndavy) or alter the regex to match spaces as well: /^[\w ]+$/ Quote Link to comment https://forums.phpfreaks.com/topic/279592-pcre-expressions/#findComment-1437988 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.