Lets say you need to display a text teaser of the actual value, but you want to have all email format to be in “*******”, say

$var = "Please email me at or at my private email";

should display as

Please email me at ******************* or at my private email ******************

How would you accomplish this? Is there a way to use str_replace to match a particular regular expression then replace that with a fix ten character asterisk? I wanted to hear your opinions :slight_smile:

preg_match_all('/[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))/', $var, $results);
foreach($results[0] as $email)
$var = str_replace($email, str_repeat('*', strlen($email));

Something like that? :wink:

for regular expression you can use: preg_replace

Thanks! That was fast, you didn’t make it look like it was tricky in any sense.

Follow up question since im not much of a preg_match user, the reg expression you provided is rather lengthy than the usual, can u explain the part where there is “aero|coop|info|museum|name”?

one more thing, you missed an additional parameter on the str_replace line :wink:

That’s probably overly lengthy in a lot of respects, but it depends on your content.
With regards to “aero|coop|info|museum|name”, it is in addition to “[a-zA-Z]{2,3}” which checks for things like .us or .com, but not, say, 6 letter TLD like museum.

More info here

Thanks hash, ill check on the link to read further. I hope to work on string manipulations more so i can learn these things while working.