سؤال

I want to create a contact form that has an email field where the user can’t enter any white space, for example: test@example.com m
I am using a spam scrubbing function to clean user inputs.
I am using my variable like so: $email = strip_tags($scrubbed['email']);

How can I add something like this: $email = preg_replace('/\s+/', '', $email); to the existing $email = strip_tags($scrubbed['email']);?

I tried $email = (preg_replace('/\s+/', '', $email)(strip_tags($scrubbed['email']))); and all sorts of variations to that. They didn’t work.

لا يوجد حل صحيح

نصائح أخرى

You should strongly consider using filter_var() for email address validation.

if(filter_var($email, FILTER_VALIDATE_EMAIL) !== false)) {
    // validation failed
}

Trying to fix input for the end user can lead to unexpected results and still not give you a valid email address.

Why don't you just do it in two lines?

$email = strip_tags($scrubbed['email']);

$email = preg_replace('/\s+/', '', $email);

Try:

$email = preg_replace('/\s+/', '', strip_tags($scrubbed['email']));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top