Why won't this line check script work?

These functions are in the class file, and they are passed by variables through the form. Why it will be indicated No coincidence if I give him a string with the letters az or AZ?

function pattern_check($patten, $string) {
$pass = preg_match($patten, $string);
return $pass;
}
function check_name($name) {
    $pat = '/^[a-zA-Z]/';
    $name = $this->pattern_check($pat, $name);
    if($name) {
        echo "Match";
    }
    else {
        echo 'No match';
    }

}
+3
source share
4 answers

The only thing you are trying to verify right now is that the first char in your line is a letter. Nothing more, nothing less.

, , '#^[a-zA-Z]+$#'. , . , . utf8, u , #^[a-zA-Z]+#u

+3

, . , . , .

:

function test($name)
{
    echo "${name}: ";
    check_name($name);
    echo "\n";
}

test(" ZZ1");
test("123");
test("4aa");
test("AAA");
test("Z11");
test("ZZ ");
test("ZZZ");
test("aaa");
test("zzz");
test("Ábc");

...

% ./test.php    

 ZZ1: No match
123: No match
4aa: No match
AAA: Match
Z11: Match
ZZ : Match
ZZZ: Match
aaa: Match
zzz: Match
Ábc: No match

% php --version
PHP 5.3.4 (cli) (built: Dec 15 2010 12:15:07) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

$name :

$name = $this->pattern_check($pat, $name);
if($name) {

:

$matched = $this->pattern_check($pat, $name);
if ($matched) {
+2

php,

$name = $this->pattern_check($pat, $name);

$name = pattern_check($pat, $name);

, ?

- , .. , ,

class mytest{


    private function pattern_check($patten, $string) {
        $pass = preg_match($patten, $string);
        return $pass;
    }
    public function check_name($name) {
        $pat = '/^[a-zA-Z]/';
        $name = $this->pattern_check($pat, $name);
        if($name) {
            echo "Match";
        }
        else {
            echo 'No match';
        }

    }

}
$obj = new mytest();
$obj->check_name('ABCDEFG');
OUTPUT -> Match
+1
source

I realized that the regular expression is wrong, the correct template for checking the name should be to make sure that it is capitalized with lowercase letters after the specified capital, therefore

[AZ] * [az] {1,50} will be true for any word starting with capital, and immediately after the capital will have lowercase letters from 1 to 50 characters.

0
source

All Articles