Hi, i need to remove all email addresses and links from a string and replace them with "[removed]" and i'm a bit lost on how to do it.
Can someone help me on this?
Thanks.
views:
84answers:
3
+1
A:
You can use preg_replace to do it.
for emails:
$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);
for urls:
$pattern = "/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);
Resources
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
Josiah
2010-07-21 19:51:48
hmm just tried and removes the whole text i had....
JEagle
2010-07-21 19:56:54
Can you post a small sample of the text?
Josiah
2010-07-21 20:00:32
It was just a random text i had. Nothing specific, just some email address and some links
JEagle
2010-07-21 20:08:12
Actually, I found the problem. Try my edited code :)
Josiah
2010-07-21 20:08:28
It works. Thanks.
JEagle
2010-07-21 20:12:44
Glad to help :)
Josiah
2010-07-21 20:17:58
+1
A:
Try this:
$patterns = array('<[\w.]+@[\w.]+>', '<\w{3,6}:(?:(?://)|(?:\\\\))[^\s]+>');
$matches = array('[email removed]', '[link removed]');
$newString = preg_replace($patterns, $matches, $stringToBeMatched);
Note: you can pass an array of patterns and matches into preg_replace instead of running it twice.
treeface
2010-07-21 19:52:57
A:
The answer I was going to upvote was deleted. It linked to a Linux Journal article Validate an E-Mail Address with PHP, the Right Way that points out what's wrong with almost every email regex anyone proposes.
The range of valid forms of an email address is much broader than most people think.
Stephen P
2010-07-21 20:04:58