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 [email protected]

 

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

[email protected]

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/219030-replace-email-address-with-xs/
Share on other sites

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

 

<?php

$email = "[email protected]";
$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 = "[email protected]";

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

?>

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

 

 

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

 

<?php

$email = "[email protected]";

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.