PHP preg_match for only numbers and letters, no special characters

后端 未结 3 1917
[愿得一人]
[愿得一人] 2021-02-06 04:04

I don\'t want preg_match_all ... because the form field only allows for numbers and letters... just wondering what the right syntax is...

Nothing fancy ... just need to

相关标签:
3条回答
  • 2021-02-06 04:36

    If you just want to ensure a string contains only alphanumeric characters. A-Z, a-z, 0-9 you don't need to use regular expressions.

    Use ctype_alnum()

    Example from the documentation:

    <?php
    $strings = array('AbCd1zyZ9', 'foo!#$bar');
    foreach ($strings as $testcase) {
        if (ctype_alnum($testcase)) {
            echo "The string $testcase consists of all letters or digits.\n";
        } else {
            echo "The string $testcase does not consist of all letters or digits.\n";
        }
    }
    ?>
    

    The above example will output:

    The string AbCd1zyZ9 consists of all letters or digits.
    The string foo!#$bar does not consist of all letters or digits.
    
    0 讨论(0)
  • 2021-02-06 04:46
    if(preg_match("/[A-Za-z0-9]+/", $content) == TRUE){
    
    } else {
    
    }
    
    0 讨论(0)
  • 2021-02-06 04:49

    If you want to match more than 1, then you'll need to, however, provide us with some code and we can help better.

    although, in the meantime:

    preg_match("/([a-zA-Z0-9])/", $formContent, $result);
    print_r($result);
    

    :)

    0 讨论(0)
提交回复
热议问题