Extract Query String Email That Has Plus (+) Sign

I have a shortcut where an email is embedded along with a code like this.

https://mydomain.com?e=first.last+more@gmail.com&cde=uiyi76#5kjuh8y77ykj

In most cases this works, except like the above case where there is a plus (+) sign in the email. I have written the following code that works, but I am curious if there is a better way. I am using PHP to process this.

$val = $_GET['e'];
if (strstr($val, "@")) $e = str_replace(" ", "+", $val);
else $e = $val; 

Open in new window


Can you either confirm this is a good way to go or suggest an alternative.

Thanks.
Paul KonstanskiProject SpecialistAsked:
Who is Participating?
 
David S.Connect With a Mentor Commented:
Thanks for supporting address tags (aka "plus addressing").

Have you tried URI encoding it?
https://mydomain.com?e=first.last%2Bmore@gmail.com&cde=uiyi76#5kjuh8y77ykj

Open in new window

or, if that doesn't work, double encoding it?
https://mydomain.com?e=first.last%252Bmore@gmail.com&cde=uiyi76#5kjuh8y77ykj

Open in new window

0
 
Paul KonstanskiProject SpecialistAuthor Commented:
These emails are coming to me this way from another application. So I do not have control over how they are encoded and sent to my application. It is an automated link to sign up for an email delivery.
0
 
David S.Connect With a Mentor Commented:
In that case, I suggest specifying the optional limit argument to str_replace, so you limit it to the first space, because only one "+" is allowed.

$val = $_GET['e'];
if (strstr($val, "@")) $e = str_replace(" ", "+", $val, 1);
else $e = $val;

Open in new window

Otherwise, the only thing you could really do is use a regular expression to check the format of the address, but such things aren't perfect.
0
 
Php_expertCommented:
Hi,

Can you try with below code if it will give output similar to sent by external system.

$val = $_GET['e'];

if (strstr($val, "@"))
      $e = urldecode($val);
else
      $e = $val;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.