How do I remove all email addresses and links from a string and replace them with "[removed]"
You can use preg_replace to do it.
$pattern = "/[^@s]*@[^@s]*.[^@s]*/"; $replacement = "[removed]"; preg_replace($pattern, $replacement, $string);
$pattern = "/[a-zA-Z]*[://]*[A-Za-z0-9-_]+.+[A-Za-z0-9./%&=?-_]+/i"; $replacement = "[removed]"; preg_replace($pattern, $replacement, $string);
PHP manual entry: http://php.net/manual/en/function.preg-replace.php
Credit where credit is due: email regex taken from preg_match manpage, and URL regex taken from: http://www.weberdev.com/get_example-4227.html
You need to place the
n in double quotes.
Inside single quotes it is treated as 2 characters
'' followed by
$str = str_replace("n", '', $str);
A better alternative is to use
$str = str_replace(PHP_EOL, '', $str);
There are two problems here:
- You are not removing the address from the mail, so when you add a second one, the first one is still there and both will see the other address.
- You are using a
headerredirect in your loop without terminating your script. This can cause code after the redirect to run, but there is no guarantee for how much and how long.
The first problem you can solve by clearing the recipients at the end of the loop:
As for the second problem, you should not redirect anywhere inside the loop and when you redirect after all messages have been sent, you should exit your scipt using
exit; so that nothing gets executed after that.
You're using it wrong:
mail($to, $subject, $body, $from, $headers)
mail() function does not have a
$from parameter. You ought to throw it into
$headers as well.
To manipulate HTML it is generally a good idea to use a DOM aware tool instead of plain text manipulation tools (think for example what will happen if you enounter variants like
<br /> with more than one space, or even
<BR/>, which altough illegal are sometimes used). See for example here: http://sourceforge.net/projects/simplehtmldom/