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 test@test.com I need the email address to show on the page like this test@xxx.com Thanks in advance Quote Link to comment 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 = "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"; ?> Quote Link to comment 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)); Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 = "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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.