Jump to content

str_replace, preg_replace?


_tina_

Recommended Posts

Hi,

 

I have an email address that I'm passing as a parameter. 

 

I need to convert email addresses that may have a plus sign "+" in them to the appropriate value.  For instance, if the user submits their email address as user+userB@email.com, I am using urlencode and on the following page, I am getting user userB@email.com which is understandable. 

 

Is there some way I can get around this?  I was thinking of something like:

if( the email has a plus in it )
  replace that with the proper code so the next page will understand and insert a + instead of a space
End if

 

I hope this makes sense and thank you in advance :)

 

 

Link to comment
Share on other sites

Depends!  Gmail and other clients ignore everything after the plus, which allows people to filter spam effectively.  If you are using this to foil people's attempt to divert spam, I don't think you deserve help :)

 

However, if you have a non malicious reason for doing this, maybe I will give you a hint.

Link to comment
Share on other sites

Can you explode(with + being the exploding point)

then implode(with @ being the implode point)

Is the email in a form field or something?

You may also be able to use ereg_match() this looks for a regular expression and if it has the "+" then explode(+) then I think you can implode(@). Maybe something like that.

Link to comment
Share on other sites

Thanks for all the replies. 

 

I have looked into urlencode, apparently though, it is urlencode which add's the "+" in the first place and rawurlencode isn't an option. 

 

I am passing the paramaters along with a link i.e.

<a href=link.com?param=1&param2=email etc.

 

There is no maliciousness here on my part!  It's just the client uses a "+" in his email address and wants to be able to do so in this system. 

 

Thanks again for all the replies.

Link to comment
Share on other sites

<?php
$string = 'spammers+areassholes@wtf.com';
$pattern = '/\+[^@]+/i';
echo preg_replace($pattern, '', $string);

 

Thanks for this.  Just one question, I gather that this will strip out the + sign and leave 'spammersareassholes@wtf.com', is this correct?  If so, how do I get the original email address, with the + sign, back on the next page?

Link to comment
Share on other sites

I agree with DanielO, why not rawurlencod()? That's what it is there for. But assuming you have a good reason (which I can't think of one) a solution would be to replace plus signs with an alternative character (that isn't used in emails) before passing on the query string. Then replace it back to plus signs on the receiving page before using. But, again, there's no logical reason not to use rawurlencode().

Link to comment
Share on other sites

I agree with DanielO, why not rawurlencod()? That's what it is there for. But assuming you have a good reason (which I can't think of one) a solution would be to replace plus signs with an alternative character (that isn't used in emails) before passing on the query string. Then replace it back to plus signs on the receiving page before using. But, again, there's no logical reason not to use rawurlencode().

 

Thanks for that, the reason I can't use rawurlencode is because I'm doing this project using the Symfony framework which is new to me and I can't it uses urlencode by default and there I can't seem to get it to use rawurlencode.  This is why I'm looking for a work around. 

 

Like I said above, maybe replacing the + in an email with a %2B might solve this. 

Link to comment
Share on other sites

How exactly are you doing it? It's just a matter of doing

$email = rawurlencode($email);

and then passing it to whatever you want.

 

That's exactly how I'm doing it ^.  It's having no effect though.  It seems to just completely ignore it.

Link to comment
Share on other sites

echo rawurlencode('foo+bar@example.com');

outputs

foo%2Bbar%40example.com

on my system, like it should. What do you mean with it doesn't have any effect?

 

Yes, it does this on my system too.  However when I do this in Symfony, it has no effect, i.e. it completely ignores it and add's a "+".

Link to comment
Share on other sites

<?php
$string = 'spammers+areassholes@wtf.com';
$pattern = '/\+[^@]+/i';
echo preg_replace($pattern, '', $string);

 

Thanks for this.  Just one question, I gather that this will strip out the + sign and leave 'spammersareassholes@wtf.com', is this correct?  If so, how do I get the original email address, with the + sign, back on the next page?

 

This removes the '+' and everything between it and the '@'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.