replace ej: hotmail.com with @hotmail.com before insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

replace ej: hotmail.com with @hotmail.com before insert

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;

?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
Last edited by Chris Corbyn on Sun Apr 24, 2005 1:49 pm, edited 1 time in total.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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 ?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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; 
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;-)
Last edited by Chris Corbyn on Sun Apr 24, 2005 2:03 pm, edited 2 times in total.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

W00ps... but yea its a typo. it also missing
;
and the ,$string :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

See edited post above ^^
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Now that is a nice one..
Since i've got rather some strings to replace.


Thanks a lot.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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
address@hotmail.com - address@yahoo.com - address@yahoo.es --Número teléfono borrado -- 3 -- 2 -- Número teléfono borrado
As you can see its dispaying 3 -- 2 instead of 123 -- 12
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Heh... it's spot the typo time :-D

$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).
Post Reply