Page 1 of 1

preg_replace

Posted: Thu Apr 07, 2005 10:42 pm
by ol4pr0
Trying to strip out the email address from a text but.. Urmm :(

Code: Select all

<?php 
$email_search = "([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})";
$email_replace = "******";
echo preg_replace($email_search, $email_replace, $row->txt);
?>

preg_replace

Posted: Fri Apr 08, 2005 12:08 am
by marike
The first parameter of preg_replace has to be the regular expression.

Code: Select all

<?php

$email_search=<<<TEXT

Name               E-Mail Address
------------------------------------------------
Inky T. Ghost      inky@pacman.example.com
Donkey K. Gorilla  kong@banana.example.com
Mario A. Plumber   mario@franchise.example.org
Bentley T. Bear    bb@xtal-castles.example.net
TEXT;

$email_replace= '*************';

print preg_replace('/[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}/',
                   $email_replace, $email_search);
?>
Hope things work out for you. Greeting from New York. P.S. Pretty much all of the answers to things like this are at php.net. Sometimes you just have to dig a little. Cheers.

Posted: Sat Apr 09, 2005 12:24 pm
by ol4pr0
Oke.. thanks. however i made a dumb mistake

the $row->text wasn't really there :( Ouch.

Inquery.

Could i do something like this by the way, I haven't tried it yet.

Code: Select all

<?php 
$replace = array("1"=>"one","2","two","([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})"=>"[***]");
echo preg_replace($replace, $row->txt);
?>

Posted: Sat Apr 09, 2005 12:53 pm
by Ambush Commander
Here's a better regexp for matching emails:

Code: Select all

^((&#1111;A-Za-z0-9]+_+)|(&#1111;A-Za-z0-9]+\-+)|(&#1111;A-Za-z0-9]+\.+)|(&#1111;A-Z
a-z0-9]+\++))*&#1111;A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.&#1111;a-z
A-Z]{2,6}$
More be found here:
http://www.regexlib.com/Search.aspx?k=email

Posted: Sat Apr 09, 2005 1:29 pm
by Chris Corbyn
ol4pr0 wrote:Oke.. thanks. however i made a dumb mistake

the $row->text wasn't really there :( Ouch.

Inquery.

Could i do something like this by the way, I haven't tried it yet.

Code: Select all

<?php 
$replace = array("1"=>"one","2","two","([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})"=>"[***]");
echo preg_replace($replace, $row->txt);
?>
Yes and no.

Yes, you can use an array(s) for the pattern and the replacement, but from the above it looks as if you're trying to put them both together in an assoc array as the pattern. Consult the manual, it's in there :D

preg_replace

Posted: Sat Apr 09, 2005 2:46 pm
by marike
ol4pr0 wrote:
"Could i do something like this by the way, I haven't tried it yet."

Code: Select all

<?php 
$replace = array("1"=>"one","2","two","([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})"=>"[***]");
echo preg_replace($replace, $row->txt);
?>
Actually the answer is no. You'll get a "Wrong parameter count for preg_replace()". preg_replace must take 3 parameters. And you'll also notice that your trying to get a property of a non-object here, assuming you haven't created a $text object.

As far a there being a "better" regex, who cares if it works, which the code I posted does. Did you, the original poster even bother to run the code I gave you?

I sometimes wonder why the php.net manual is not the first place people look.