警告:preg_match():未知修饰符'('在第18行

时间:2012-11-25 19:05:36

标签: php regex

我在这个问题的标题中收到了错误。帮我查一下我的联系表单中的错误:

<?php

//Prefedined Variables
$to = "example@example.com";
$subject = "1";

if($_POST) {
    // Collect POST data from form
    $name = stripslashes($_POST['name']);
    $email = stripslashes($_POST['email']);
    $comment = stripslashes($_POST['comment']);

    // Define email variables
    $message = date('d/m/Y')."\n" . $name . " (" . $email . ") sent the following comment:\n" . $comment;
    $headers = 'From: '.$email.'\r\n\'Reply-To: ' . $email . '\r\n\'X-Mailer: PHP/' . phpversion();

    //Validate
    $header_injections = preg_match("(\r|\n)(to:|from:|cc:|bcc:)", $comment); 

    if( ! empty($name) && ! empty($email) && ! empty($comment) && ! $header_injections ) {
        if( mail($to, $subject, $message, $headers) ) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}
?>

问题似乎在这里,但我不明白什么是错的!

$header_injections = preg_match("(\r|\n)(to:|from:|cc:|bcc:)", $comment);

2 个答案:

答案 0 :(得分:3)

尝试:

$header_injections = preg_match("#(\r|\n)(to:|from:|cc:|bcc:)#", $comment); 

您必须在正则表达式的开头和结尾处提供有效的符号,在此示例中仅为#,但您可以使用/或任何您想要的内容。

看一下这篇文章:RegEx delimiters

答案 1 :(得分:0)

尝试使用:

$header_injections = preg_match('/(\r|\n)(to:|from:|cc:|bcc:)/', $comment); 

同样在您的IF情况下,您应该以这种方式检查$header_injections

if( ! empty($name) && ! empty($email) && ! empty($comment) && FALSE !== $header_injections ) {

由于preg_match可以返回可以转换为布尔值的值并跳过验证。

相关问题