preg_replace() ?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
vietboy505
Forum Commoner
Posts: 53
Joined: Wed Feb 22, 2006 9:30 am

preg_replace() ?

Post by vietboy505 »

Should I use preg_replace to search for symbols I don't like in a string?

Code: Select all

$a=1234567891@mail.com
$b=a b 9876543210
$c=1-1234567890
$d=123-456-7891

$a=preg_replace('/\s+/', '', trim(a)); 
$a=preg_replace('1-', '',$a); 
$a=preg_replace('(', '',$a); 
$a=preg_replace(')', '',$a); 
$a=preg_replace('@mail.com', '',$a); 
$a=preg_replace('@', '',$a); 
$a=preg_replace('.', '',$a);
I was wondering if there is a way I can get just those 10 digits only so I don't have to go each string?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$var = preg_replace('#[^\d]+#', '', $var);
Post Reply