2012-05-16 41 views
0
preg_match("/[\]/",'',$body); 

我使用的preg_match但我这里有这个错误的preg_match错误信息

编译失败:缺少终端对于字符类偏移量为3

什么是错在这里我无法找到它

$email_message = "Form details below.\n\n"; 

        function clean_string($string) { 
         $bad = array("content-type","bcc:","to:","cc:","href"); 
         return str_replace($bad,"",$string); 
        } 

        $email_message .= "Full Name: ".clean_string($_POST['full_name'])."\n"; 
        $email_message .= "Email: ".clean_string($_POST['email'])."\n"; 
        $email_message .= "Telephone number: ".clean_string($_POST['telephone'])."\n"; 
        $email_message .= "Message: ".clean_string($_POST['comments'])."\n"; 


        $mail    = new PHPMailer(); 
        $body    = $email_message; 
        $body    = str_replace('\\', '', $body); 

        $mail->IsSMTP(); // telling the class to use SMTP 
        $mail->SMTPAuth = true;      // enable SMTP authentication 
        $mail->SMTPSecure = "ssl";      // sets the prefix to the servier 
        $mail->Host  = "smtp.gmail.com";   // sets GMAIL as the SMTP server 
        $mail->Port  = 465;      // set the SMTP port for the GMAIL server 
        $mail->Username = "[email protected]";  // GMAIL username 
        $mail->Password = "hinadk";  // GMAIL password 

        $mail->SetFrom('[email protected]', 'First Last'); 

        $mail->Subject = "Imperia"; 

        $mail->AltBody = "To view the message, please use an HTML compatidble email viewer!"; // optional, comment out and test 

        $mail->MsgHTML($body); 

        $address = "[email protected]"; 
        $mail->AddAddress($address, "To Name"); 

        if(!$mail->Send()) { 

         echo "<font color='red'> Message error </font>". $mail->ErrorInfo; 
        } else { 
         echo "<font color='red'> Message sent </font>"; 
        } 

有eregi_replace(“[]”,'',$ body);

,但我有一个为这个原因,我改变它的preg_match

+0

您试图匹配什么?我喜欢[RegExr](http://gskinner.com/RegExr/)工具 – message

+0

我刚刚更新了 – fish40

回答

1

你正在逃避最后一个支架,但不是第一个。要匹配反斜杠,你可以这样做:

preg_match('/\\\\/', '', $body); 

从您的编辑,似乎您试图从$body删除反斜杠。在这种情况下,您需要使用preg_replace。然而,因为你做一个简单的搜索和替换,你会更好只使用str_replace

str_replace('\\', '', $body); 
+0

有eregi_replace(“[]”,'',$ body); 但我有另一个错误,因为这个原因我把它改为preg_match – fish40

+0

'eregi_'函数已被弃用,所以你不能使用这些。但是'preg_match'不能替代,只能搜索。因此,您应该使用'preg_replace'或'str_replace'。 – webbiedave

+0

对不起兄弟,我应该在这里写什么preg_replace(“[]”,'',$ body)? – fish40

0

\用于转义特殊字符,所以你逃避你的右括号另一个错误,你有一个左括号没有关闭,主导到一个错误。

你想要完全匹配什么,\角色?

0

中的preg_match的第三个参数是匹配数组,这样你将覆盖$体变量的值数组。 替代使用preg_replace而不是preg_match $ body = preg_replace(“/ [\] /”,'',$ body); 并在正则表达式中使用转义