Page 1 of 1
replace ej: hotmail.com with @hotmail.com before insert
Posted: Sun Apr 24, 2005 1:40 pm
by ol4pr0
Ok..
I do not allow emails to be included in the text.
However there is always somebody that is trying to get around the issue.
So now my following problem.
They figured out that if they use addresshotmail.com is will still be inserted.
And in the output i only check if its written like
address@hotmail.com
How can i replace addresshotmail.com with
address@hotmail.com when its being inserted into the database. And ofcourse lots of other email address extensions.
I would also like to replace phone numbers. ( an integer longer than 3 would be sufficient ) Since there can be a age or experiance years specified in the text.
Posted: Sun Apr 24, 2005 1:43 pm
by Chris Corbyn
Code: Select all
$string = "some text here and my email is joebloggshotmail.com - email me!";
$insert_version = preg_replace('/(\b\w*?)hotmail\.com\b/is', "$1@hotmail.com", $string);
echo $insert_version;
?>
Posted: Sun Apr 24, 2005 1:48 pm
by Chris Corbyn
I'd also do this too:
Code: Select all
$string = 'My address is joebloggs at hotmail.com';
$insert_version = preg_replace('/(\b\w*)\s*at\s*hotmail\.com\b/is', "$1@hotmail.com", $string);
Posted: Sun Apr 24, 2005 1:49 pm
by ol4pr0
Thanks..
( i have seen some link to some page a couple ago.. )
About all those regex.. but can't find it..
ps: you have the answer on how to replace a digit longer than 3 integers ?
Posted: Sun Apr 24, 2005 1:51 pm
by Chris Corbyn
ol4pr0 wrote:ps: you have the answer on how to replace a digit longer than 3 integers ?
Code: Select all
$string = 'My cell mobile is 123456789, keep it!';
$insert_version = preg_replace('/\d{3}\d+/s', '********', $string);
echo $insert_version;
EDIT | =>>
http://www.regular-expressions.info/
http://www.perl.com/doc/manual/html/pod/perlre.html
Posted: Sun Apr 24, 2005 1:55 pm
by ol4pr0
so this would be fine..
Code: Select all
<?
$string = $row->text;
$search = array('/(\b\w*)\s*at\s*hotmail\.com\b/','/(\b\w*?)hotmail\.com\b/','/(\b\w*?)yahoo\.com\b/');
$replace = array("$1@hotmail.com","$1@hotmail.com","$1@yahoo.com","$1@yahoo.es")
echo $string;
?>
Posted: Sun Apr 24, 2005 1:57 pm
by Chris Corbyn
LOL... yeah (If you had put the preg_replace() in there, but i know that was just a typo :p)
On another note....
If you're using arrays like that, because they're replaced by the respective value in the other array, to avoid things getting mixed up do this.
Code: Select all
//original => replace
$patterns = array (
'/(f)oo/' => "bar$1",
'/\w+(\d+)/' => "$1"
); //etc etc
//then:
$orignal = array();
$replace = array();
foreach ($patterns as $orig => $repl) {
$original[] = $orig;
$replace[] = $repl;
}
preg_replace($original, $replace, $string);
I know it tedious when first written but all you have to do after that is add to the patterns array

Posted: Sun Apr 24, 2005 2:00 pm
by ol4pr0
W00ps... but yea its a typo. it also missing
;
and the ,$string

Posted: Sun Apr 24, 2005 2:05 pm
by Chris Corbyn
See edited post above ^^
Posted: Sun Apr 24, 2005 2:10 pm
by ol4pr0
Now that is a nice one..
Since i've got rather some strings to replace.
Thanks a lot.
Posted: Sun Apr 24, 2005 2:24 pm
by ol4pr0
Small problem.
Code: Select all
<?
$string = "addresshotmail.com - addressyahoo.com - addressyahoo.es --12345 -- 123 -- 12 -- 1234";
$patterns = array (
'/(\b\w*?)hotmail\b/' => "$1@hotmail",
'/(\b\w*?)yahoo\b/' => "$1@yahoo",
'/\d{3}\d+/s' => "<font color=\"red\">Números de teléfono borrado</font>",
'/\w+(\d+)/' => "$1");
$orignal = array();
$replace = array();
foreach ($patterns as $orig => $repl) {
$original[] = $orig;
$replace[] = $repl;
}
echo preg_replace($original, $replace, $string);
?>
Output
As you can see its dispaying 3 -- 2 instead of 123 -- 12
Posted: Sun Apr 24, 2005 2:49 pm
by Chris Corbyn
Heh... it's spot the typo time
$orignal = array() should be $original = array();
Also, you're getting that since \w can be any number, letter or underscore. Take out the \w(\d+) part (it was meaninless example i put in my script above).