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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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