Simon Suh
Simon Suh

Reputation: 10882

How do I add in string values in the middle of another string using PHP?

I have an automated system displaying certain e-mails. To prevent spam bots from picking up the e-mails I would like to make a php script that automatically adds a '[REMOVE THIS]' string right before the '@' sign in an e-mail.

substr_replace() and strpos() don't work because substr_replace requires that the replacing string be the same length as the original piece of string, and strpos() can only replace one letter/symbol at a time. I need to be able to add in a whole new piece of string, '[REMOVE THIS]', while not deleting anything from the original string.

How do I do this?

Upvotes: 1

Views: 457

Answers (3)

Rahly
Rahly

Reputation: 1511

preg_replace() preg_replace("/\@/", "[REMOVE THIS]@", $emailaddress)

Upvotes: 0

Latox
Latox

Reputation: 4695

In other words, [email protected] becomes name[REMOTE THIS]@site.com ?

<?php
$email = "[email protected]";
echo str_replace("@", "[REMOVE THIS]@", $email);
?>

Or, you can use :

http://www.wbwip.com/wbw/emailencoder.html

Type in your e-mail, get the encoded version, put it in your site and it will display your proper e-mail and bots won't be able to grab it, as its just code although it echos out the real result.. but when bots searching through the code, all they will see is code.

Upvotes: 1

Michael
Michael

Reputation: 2631

As simple as this:

$obfuscated_email = str_replace('@', '[REMOVE THIS]@', $real_email);

and back again:

$real_email = str_replace('[REMOVE THIS]', '', $obfuscated_email);

Upvotes: 0

Related Questions