All non alphanumeric but also allow _

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

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

All non alphanumeric but also allow _

Post by shiznatix »

Just as the title states, I need to strip out everything that is not alphanumeric but I need to allow the _ character through. How do I do this?
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

$data = preg_replace('#[^a-z_]+#i', '', $data);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

\W (Uppercase) ?

Code: Select all

preg_replace('/\W+/', '', $string);
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ahha and life is now easy
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

d11wtq's worked the charm. feyd sorry but yours works perfect unless it is a single digit that goes it, if it is just a single digit it turns it to null.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

oops, I forgot to add numbers. :P

Code: Select all

$data = preg_replace('#[^a-z0-9_]+#', '', $data);
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

silly silly. How would I edit that to allow a blank space though as well?
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

To allow space use: (I THINK! don't bite me if i'm wrong)

Code: Select all

$data = preg_replace('#[^a-z0-9 _]+#', '', $data);
Just basicly add anything you want inbetween the [ and ]

If you wanted to allow the ; char. :

Code: Select all

$data = preg_replace('#[^a-z0-9;_]+#', '', $data);
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

thanks mate ill give that a shot
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

my brain must be off today, I forgot the i modifier for the pattern in the updated one. :roll:

Code: Select all

$data = preg_replace('#[^a-z0-9 _]+#i', '', $data);
with space
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:my brain must be off today, I forgot the i modifier for the pattern in the updated one. :roll:

Code: Select all

$data = preg_replace('#[^a-z0-9 _]+#i', '', $data);
with space
Or even easier:

#[^\w ]+#

:P

No "i" needed :lol: I never get why people use a-z0-9_ when that's precisely what \w is :idea:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

d11wtq wrote:
feyd wrote:my brain must be off today, I forgot the i modifier for the pattern in the updated one. :roll:

Code: Select all

$data = preg_replace('#[^a-z0-9 _]+#i', '', $data);
with space
Or even easier:

#[^\w ]+#

:P

No "i" needed :lol: I never get why people use a-z0-9_ when that's precisely what \w is :idea:
You're mistaken.. ;)

Code: Select all

<?php

$str = '';

for($i = 0; $i < 256; $i++)
{
	$str .= chr($i);
}

echo preg_replace('#[^\w]#', '', $str);

?>

Code: Select all

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzâèîÄÜ£&#8359;ƒ¬&#9619;&#9474;&#9569;&#9571;&#9553;&#9492;&#9524;&#9516;&#9500;&#9472;&#9532;&#9566;&#9567;&#9562;&#9556;&#9577;&#9574;&#9568;&#9552;&#9580;&#9575;&#9576;&#9572;&#9573;&#9561;&#9560;&#9554;&#9555;&#9578;&#9496;&#9484;&#9608;&#9604;&#9612;&#9616;&#9600;&#945;ß&#915;&#960;&#931;&#963;µ&#964;&#934;&#920;&#937;&#948;&#8734;&#966;&#949;&#8745;&#8801;±&#8805;&#8804;&#8992;&#8993;÷°&#8729;·&#8730;&#8319;²&#9632;
Post Reply