• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

**Programming**: Parsing email addresses with regular expressions

CaptainKahuna

Platinum Member
I'm working in PHP - trying to parse the input of this form field:

"Enter as many email addresses as you want, separating with a single space."

However, I know users are stupid, so I need to be able to handle the input even if they separate with commas, commas and spaces, three spaces, etc. So I came up with the following statement:

$explodedAddresses = preg_split("/[\s,]+/", $allInvites);

Where $allInvites is the raw contents of the textarea from the form.

I then use the email addresses to build activation links for the recipients of the emails, which are in the following form -

/acceptInvite.php?email=example@test.com&code=xxxxxxxx - where code is their activation code, randomly generated 8 letter/number sequence.

The URL is then emailed to example@test.com

However - I'm having the following problem. Sometimes, and I can't determine what the pattern is, the first two letters of the email address (and sometimes the code) get cut off, along with the equals sign, leaving the link in the following form -

/acceptInvite.php?emailample@test.com&codexxxxxx

Here is the line of PHP that builds the link -
$link .= "\n\n /acceptInvite.php?email=$inviteMem->mem_email&code=$password \n\n";

What perplexes me however, is that the email gets delivered successfully, to the correct (non-truncated) address and I use the same variable to send the email as build the link. So my best guess is that it's some kind of character encoding issue? I really don't know. The PHP file is in "Western ASCII" encoding, with Unix line endings, on a Linux server.

Thanks for reading - any ideas? Let me know if you need more info.
 
Regardless of whether I use post or get, I still need to split the user input in the text area into the individual email addresses...
 
i think you'd be better off replacing all spaces and semicolons with commas
then explode it into an array
than loop through the array and validate each address format and send the email there
 
For the sake of the archives, I figured it out - here's the solution:

You must replace all the "=" signs with "=3D". I don't know why, but it works.
 
Look for open source projects. I found a great one where you can give it a sample regular expression and a test string to see what it does.

I found an awesome one written in Java!
 
Back
Top