matghazaryan
matghazaryan

Reputation: 5896

preg_match error message

preg_match("/[\]/",'',$body);

I use preg_match but I have this error here

Compilation failed: missing terminating ] for character class at offset 3

What's wrong here I can't find it

$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>";
                    } 

there was eregi_replace("[]",'',$body);

but I had a another error for that reason i change it to preg_match

Upvotes: 0

Views: 249

Answers (3)

Zorg
Zorg

Reputation: 96

The third parameter of the preg_match is the matches array, in this way you overwrite the $body variable's value by an array. for replacing use preg_replace instead of preg_match $body = preg_replace("/[\]/",'',$body); and use escaping in regular expression

Upvotes: 0

webbiedave
webbiedave

Reputation: 48897

You're escaping the last bracket but not the first. To match backslash, you can do:

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

From your edit, it appears you're trying to remove backslashes from $body. In this case, you would need to use preg_replace. However, since your doing a simple search and replace, you'd be better off just using str_replace:

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

Upvotes: 1

jeroen
jeroen

Reputation: 91744

The \ is used to escape special characters, so you are escaping your closing bracket and you have an opening bracket without the closing one, leading to an error.

What do you want to match exactly, the \ character?

Upvotes: 0

Related Questions