Jump to content

Replace email address with X's


timmah1

Recommended Posts

I need to replace everything after the @, but before the . with X's on a page.

 

I can remove the email with this

$result = substr($email, 0, stripos($email, "@") );

 

but now how do I go about replacing the @domain.com with X's?

 

Example:

Suppose the email address is test@test.com

 

I need the email address to show on the page like this

test@xxx.com

 

Thanks in advance

Link to comment
Share on other sites

Assuming you don't want functionality for leaving say, a .net instead of a .com, this should work:

 

<?php

$email = "test@domain.com";
$replace = "@xxx.com";

$result = str_replace(strstr($email, "@"), $replace, $email);

echo $result;

?>

 

If you are using PHP 5.3 you could do something even better such as:

 

<?php

$email = "test@domain.com";

echo strstr($email, "@", true)."@xxx.com";

?>

Link to comment
Share on other sites

Thanks everybody, this should work.

 

I was trying to figure out a way to actually have the script count the characters of the domain and show that many x's.

 

For instance, if it's yahoo.com, then show it xxxxx.com, and then if it's aol.com, xxx.com, hotmail.com, xxxxxxx.com.

 

Everything is pulled from the database.

 

Thanks

 

 

Link to comment
Share on other sites

I'm sure this could be adapted a bit, and possibly not use regex for speed, but accomplishes the goal:

 

<?php

$email = "test@test.com";

preg_match("/(.+?)\@(.+?)\.(.+?)$/", $email, $domain);

$replacement = "{$domain[1]}@".str_pad("", strlen($domain[2]), "x").".{$domain[3]}";

echo $replacement;

?>

 

Another, more efficient way to do this, would be to use and array of domains, and then have a default.

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.