How to validate that a string only contain lowercase letters?

后端 未结 3 831
日久生厌
日久生厌 2020-12-04 01:40

I\'m trying to verify that my string matches a pattern. That is, the full string can be written as that pattern. However preg_match returns true, i

3条回答
  •  旧时难觅i
    2020-12-04 02:08

    I super-love regex, but not for this task -- it is needless overhead because there is a single, direct, non-regex equivalent. https://www.php.net/manual/en/function.ctype-lower.php

    Code: (Demo)

    $tests = [
        'Test',
        'test',
        'test1',
        '1test',
        'test!'
    ];
    foreach ($tests as $test) {
        echo "$test: " , (ctype_lower($test) ? 'true' : 'false') , "\n";
    }
    

    Output:

    Test: false
    test: true
    test1: false
    1test: false
    test!: false
    

    p.s. For the record, an empty string returns false. I'm not sure of your logical validation requirements for this test unit.

提交回复
热议问题