PHP Regular Expression Help

Hi ;D! I was working on a contact page and I would like to add a feature in which it redirects the user back to the form for an incorrect E-Mail format. I already have this code, but it comes up with an error similar to this:

Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in C:\xampp\htdocs\contactsend.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\contactsend.php:23) in C:\xampp\contactsend.php on line 24

This is my code:

[php]
$email = trim($_REQUEST[‘email’]);
$pattern = “^(0-9a-zA-Z@([0-9a-zA-Z][-\w][0-9a-zA-Z].)+[a-zA-Z]{2,9})$”;
if (preg_match($pattern, $email) === false){
header( ‘Location: contact.php?redirect=3’);
exit;
}
[/php]

I would like to know what was wrong with my code and how I could fix it, thanks!

Error on line 23 is because testing for equal is == not === !
And, header error is most likely because you are trying to do this inside of HTML.

If you create a HTML page and have PHP inside of it, you can NOT send a second header.
If you need to do redirections by PHP, you need to have the HTML FORM post out to a second
PHP page which would validate the inputs and THEN redirect using the header.

If that does not make sense, then post more of your code for us to help you with… Good luck…

OK, I got my code fixed to remove any HTML from the page and I found an error in my text, missing a parenthesis.
Here’s my code now:
[php]
$pattern = “^(0-9a-zA-Z@([0-9a-zA-Z][-\w][0-9a-zA-Z].)+[a-zA-Z]{2,9})$”;
if ((preg_match($pattern, $email) == false) {
header( ‘Location: contact.php?redirect=3’);
exit;
}
[/php]
But when I run it, it comes up with this error code :frowning: :

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\contactsend.php on line 23

Any ideas on how to fix it? Thanks! :smiley:

Hello zeldarules8, i have checked your code and found that your missing one small close brackets. you are using following code if ((preg_match($pattern, $email) == false) which is wrong.

use below code
[php]
$pattern = “^(0-9a-zA-Z@([0-9a-zA-Z][-\w][0-9a-zA-Z].)+[a-zA-Z]{2,9})$”;
if ((preg_match($pattern, $email) == false)) {
header( ‘Location: contact.php?redirect=3’);
exit;
}
[/php]

i hope this will helpful for you and resolve your issue
Reply your feedback and other issues.
SR

:smiley: Thanks! I got the code running smoothly now!

hello zeldarules8
it’s really nice that your issue is finally resolve. :slight_smile: :slight_smile: :slight_smile:

SR

Sponsor our Newsletter | Privacy Policy | Terms of Service