0

Hi, I have no knowledge of preg_match(). I was wondering on how to detect if the text I send to the function, and only allow the text like this:

sample1127

Like that, with no underscores or anything. Just text and numbers.

I would be happy if you helped...

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by Djmann1013
1

Here is one solution to your problem:

<?php

    function checkPreg($string)
    {
        if(preg_match('/^[^\W_]+$/', $string)):
          return true;
        else:
          return false;
        endif;
    }

    $string = "Hello244";

    if(checkPreg($string))
    {
        echo 'Yes, its ok';
    }else{
        echo 'No, its not ok';
    }


?>

Just use regex with the preg_match, you got the right function.

BUT, it might actually be easier just to use the pre-built in functions (for basic numbers and letters...) Here is an example:

<?php

    $string = "asfsf_";

    if(ctype_alnum($string))
    {
        echo "The string contains letters and numbers";
    }else{
        echo "The string contains chatacters that are not allowed"; 
    }
?>
Votes + Comments
Thanks
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.