preg_replace

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

preg_replace

Post 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);
?>
marike
Forum Newbie
Posts: 24
Joined: Thu Mar 31, 2005 6:19 pm
Location: New York

preg_replace

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

Post 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);
?>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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
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: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
marike
Forum Newbie
Posts: 24
Joined: Thu Mar 31, 2005 6:19 pm
Location: New York

preg_replace

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