Where in this preg_match is it saying no empty value?

Where in this Clean Email preg_match is it saying no empty value?

// Clean Email
if (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$/i",$email)) {
    header( "Location: $errorurl" );
    exit ;
}

I already use this statement to clean empty values so I would like to remove it from the above statement. Do you know how?

// Clean All
if (empty($name) || empty($email) ||empty($comments)) {
	header( "Location: $errorurl" );
	exit ;
}

Let’s break apart your regex

^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$

^ means, what follows should be at the beginning of the string provided
[A-Z0-9._%-]+ means to find 1 or more occurrences of letters, numbers, ., _, %, or -
@ means to find the @ symbol after you find one or more of the occurrences of the line above.
[A-Z0-9.-]+ means to find 1 or more occurrences of letters, numbers, ., or - following the @ symbol
\. means to find a . after the line above
[A-Z]{2,4}$ lastly, find 2 to 4 characters that are after the . and are at the end of the string

So you can deduce by the above explanation, that you won’t get a match for “” because it fails to find 1 or more characters at the beginning of a string that are letters, numbers, ., _, %, or -. Plus it fails to have the @ in after find those said beginning letters and it fails to have the required . before the suffix of the domain (which also must exist as the end of the string)

Uhmmm… Ok… So how should it be rewritten to say if empty ignore all - and if value entered clean as usual?

// Clean Email
if (!empty($email) && !preg_match("/^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$/i",$email)) { // verify the email is not empty, check if it passes the regex
    header( "Location: $errorurl" );
    exit ;
}

If I’m reading this wrong please tell me, but I need it to do NOTHING if empty and then CLEAN as usual if a value is entered?

The code states, only if there is a value in $email, run it against the preg_match expression, otherwise, continue on with the script execution.

Ahhh ok I’m good to go then! Thank you very much for your help. :slight_smile: