filter_var using FILTER_VALIDATE_REGEXP

前端 未结 1 1603
后悔当初
后悔当初 2021-01-08 00:09

I\'m practicing my beginner php skills and would like to know why this script always returns FALSE?

What am i doing wrong?

$namefields = \'/[a-zA-Z\\         


        
相关标签:
1条回答
  • 2021-01-08 00:46

    The regexp should be in an options array.

    $string = "Match this string";
    
    var_dump(
        filter_var(
            $string, 
            FILTER_VALIDATE_REGEXP,
            array(
                 "options" => array("regexp"=>"/^M(.*)/")
            )
        )
    ); // <-- look here
    

    Also, the

    $namefields = '/[a-zA-Z\s]/';
    

    should be rather

    $namefields = '/[a-zA-Z\s]*/'; // alpha, space or empty string
    

    or

    $namefields = '/[a-zA-Z\s]+/'; // alpha or spaces, at least 1 char
    

    because with the first version I think you match only single-character strings

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