user2415266 user2415266 - 1 year ago 72
PHP Question

PHP cuts off email address string copied from Outlook

I have a form which is used to send emails via PHP.
Now I had a user who said the email didn't go through. I checked, it appeared that only a name was saved in the field for the email address, not an email address.

So I added a client-side validation via regex which only allows you to send if the email has the format [text]@[text].[text] - common practice for validating email addresses.

The used regex:

var regex = /\S+@\S+\.\S+/;

Guess what? The problem still occurs: user can send an email, the address passes the validation, but it only saves a name!

But how?

The user had copied the email address from Outlook, which comes in the following format:

Mustermann, Max <>

Regex validation says: that's a valid email address.

At first I thought I had to change my regex, to not allow this. But one of my co-workers says this is a valid format for email adresses.

So I started looking into what PHP actually did with that string. And here it becomes strange:

$email = "Mustermann, Max <>";
// prints out:
// string(42) "Mustermann, Max "

The string stops at the
, but its length is still 42. What exactly happens here?

Answer Source

PHP does not cut anything off. but the < and > are interpreted as HTML-tags and therefore omitted by your browser. just look at the page source code and you will find your missing chars.

to see the whole text in HTML, you have to escape it:

echo htmlentities($email);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download