Jump to content

pcre expressions


phpboy07

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/279592-pcre-expressions/
Share on other sites

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 ]+$/
Link to comment
https://forums.phpfreaks.com/topic/279592-pcre-expressions/#findComment-1437988
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.