timmah1 Posted November 18, 2010 Share Posted November 18, 2010 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 More sharing options...
spfoonnewb Posted November 18, 2010 Share Posted November 18, 2010 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"; ?> Link to comment https://forums.phpfreaks.com/topic/219030-replace-email-address-with-xs/#findComment-1135894 Share on other sites More sharing options...
BRADERY Posted November 18, 2010 Share Posted November 18, 2010 $filename = 'emails.txt'; $file = file($filename); foreach($file as $cwb) { list($email,$domain) = explode('@',trim($cwb)); $emails[] = $email . '@xxx.com'; } file_put_contents($filename,implode("\n",$emails)); Link to comment https://forums.phpfreaks.com/topic/219030-replace-email-address-with-xs/#findComment-1135896 Share on other sites More sharing options...
BRADERY Posted November 18, 2010 Share Posted November 18, 2010 thats assuming you have the emails in a text with one email on each line lol Link to comment https://forums.phpfreaks.com/topic/219030-replace-email-address-with-xs/#findComment-1135897 Share on other sites More sharing options...
timmah1 Posted November 18, 2010 Author Share Posted November 18, 2010 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 https://forums.phpfreaks.com/topic/219030-replace-email-address-with-xs/#findComment-1135901 Share on other sites More sharing options...
spfoonnewb Posted November 18, 2010 Share Posted November 18, 2010 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. Link to comment https://forums.phpfreaks.com/topic/219030-replace-email-address-with-xs/#findComment-1135910 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.