preg_match中的转义+符号


Escaping + symbol in preg_match

我在转义'6+ months'中的加号时遇到问题。我试过反斜杠,但它对它没有影响。

function degree_of_interest()
{
    $program = $_REQUEST['degree_of_interest'];  
    $highest_level = $_REQUEST['college_1_degree'];
    $start = $_REQUEST['start_date'];
    if( !preg_match("/'A(1-2 months|3-4 months|6+ months)'Z/i",$start)
     OR !preg_match("/'A(Bachelors)'Z/i",$highest_level)
     OR !preg_match("/'A(JD '(Juris Doctor'))'Z/i",$program))
    {
        $this->errorsArray['degree_of_interest'][] = "For $program you must have a Bachelors degree";
    }
}

使用字符串时,除了正则表达式转义规则外,还必须遵守所有字符串转义规则。

这意味着您必须转义。正则表达式(作为字符串)应该是:

"/''A(1-2 months|3-4 months|6''+ months)''Z/i"