警告:preg_Match():在中找不到结束分隔符‘^’

时间:2019-05-25 作者:Louis Procaccini

我们收到此错误:

警告:preg\\u match():在/home/goose/public\\html/wp-content/themes/goose/partials/contact中找不到结尾分隔符“^”。php第32行请输入有效的电子邮件地址

http://thegoosedarien.com - 使用联系人表单,我们在提交时收到此错误消息。

不知道如何解决这个问题。代码中缺少什么?

<?php 

$asset_path = get_bloginfo(\'template_url\');

?>

<div class="left">

<span class="main-title">
    <?php _e( \'Contact Us\', \'goose\' ); ?>
</span>

<?php 

    if($_SERVER[\'REQUEST_METHOD\'] === \'POST\') { 

        $flag = 1; 

        echo \'<div class="notice">\';

        if($_POST[\'submission_name\'] == \'\') { 
            $flag = 0; 
            echo "Please enter your name<br />";
        } else if(!preg_match(\'/[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*/\', 
$_POST[\'submission_name\'])) { 
            $flag=0; 
            echo "Please enter a valid name<br />"; 
        } 

        if($_POST[\'submission_email\'] == \'\') { 
            $flag = 0; 
            echo "Please enter your email address<br />";
        } else if(!preg_match("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a- 
   z0-9-]+)*(.[a-z]{2,3})$", $_POST[\'submission_email\'])) { 
            $flag=0; 
            echo "Please enter a valid email address<br />";
        } 

        if($_POST[\'submission_message\'] == \'\') { 
            $flag = 0; 
            echo "Please enter a message<br />";
        }

        if (empty($_POST)) { 

            echo "Sorry, your submission is invalid - please try again!"; 
            exit; 

        } else { 

            if($flag == 1) { 
                wp_mail(get_option("admin_email"), 
trim($_POST[submission_name])." sent you a message from " . 
get_option("blogname"), stripslashes(trim($_POST[submission_message])),"From: ".trim($_POST[submission_name])." <".trim($_POST[submission_email]).">\\r\\nReply-To:".trim($_POST[submission_email]));
                echo "Submission successfully sent!<br />";
            }

        }

        echo \'</div>\';

    }

?>

<form method="post" parsley-validate parsley-show-errors="false" 
id="contact-form">
    <div class="line">
        <label for="name"><?php _e( \'Your Name\', \'goose\' ); ?></label>
        <input type="text" id="name" name="submission_name" required="required" />
    </div>
    <div class="line">
        <label for="email"><?php _e( \'Your Email\', \'goose\' ); ?></label>
        <input type="text" id="email" name="submission_email" required="required" parsley-type="email" />
    </div>
    <div class="line">
        <label for="msg"><?php _e( \'Message\', \'goose\' ); ?></label>
        <textarea id="msg" cols="10" rows="10" name="submission_message" required="required"></textarea>
    </div>  
    <button>
        <span><?php _e( \'Submit\', \'goose\' ); ?></span>
    </button>
</form>

2 个回复
SO网友:Antti Koskinen

我通常尽量避免使用preg_match(), 因为我很少正确地理解模式。也许您可以尝试使用其他一些函数来进行验证。例如PHPctype_alnum() 用于名称字段和WPis_email() 用于电子邮件字段。就像这样,

if ( $_POST[\'submission_name\'] == \'\' ) { 
  $flag = 0; 
  echo "Please enter your name<br />";
} else if ( ! ctype_alnum( $_POST[\'submission_name\'] ) ) { // ctype_alnum returns TRUE if every character in text is either a letter or a digit, FALSE otherwise. 
  $flag = 0; 
  echo "Please enter a valid name<br />"; 
} 

if ( $_POST[\'submission_email\'] == \'\' ) {
  $flag = 0; 
  echo "Please enter your email address<br />";
} else if ( ! is_email( $_POST[\'submission_email\'] ) ) {
  $flag = 0; 
  echo "Please enter a valid email address<br />";
}
此外,如果您想wp_mail() 函数更具可读性,您可以这样做,

$name = sanitize_text_field( $_POST[\'submission_name\'] );
$email = sanitize_email( $_POST[\'submission_email\'] );
$subject = sprintf( 
  \'%s sent you a message from %s\',
  $name,
  get_option("blogname")
);
$message = sanitize_textarea_field( $_POST[\'submission_message\'] );
$from = sprintf(
  "From: %s <%s>\\r\\n",
  $name,
  $email,
);
$reply_to .= sprintf(
  "Reply-To: %s",
  $email,
);
$headers = $from . $reply_to;
wp_mail(
  get_option("admin_email"),
  $subject,
  $message,
  $headers,
);

SO网友:Yuri

这很古老,但答案可能会帮助其他人。

使用正则表达式时,you can use different delimiters for the expression:

使用PCRE函数时,要求图案用分隔符括起来。分隔符可以是任何非字母数字、非反斜杠、非空白字符。有效分隔符之前的前导空格将被自动忽略。

通常,我们使用/ 将其划分为/(\\w)+/g. 但其他角色^ 也允许。

正如我们在代码中看到的,第一个preg\\u match调用使用/\'s作为分隔符:/[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*/, 但第二个preg\\u match调用根本没有分隔符:^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$.

因此,PHP正在查看表达式中的第一个字符(^) 并认为它是一个分隔符。当它找不到^ 在表达式末尾,它抛出警告No ending delimiter \'^\' found.

要解决此问题,只需向正则表达式中添加分隔符:

preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/", $_POST[\'submission_email\'])
这应该足够了。

相关推荐

Calculations in functions.php

我为自己创建了一个发票主题,并试图在自定义列中获取发票总额。我已经尝试调整模板页面中使用的代码,以便在函数中显示这一点。php文件,但它不工作。我的乘法操作数出现了一个操作数错误,我不知道为什么。这是我的代码(如有任何帮助,将不胜感激)。if( $column_name == \'invoice_total\' ) { $hours = 0; // This allows counting of each sub_field //* Set repeater va