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

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"; 
    }
?>
commented: Thanks +2

@phorce Thanks! This helped!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.