Hopefully simple question

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
Termina
Forum Newbie
Posts: 18
Joined: Sat Apr 10, 2004 11:17 pm

Hopefully simple question

Post by Termina »

I'd like a function that takes a variable, and removes certain charachters. I looked at explode, but I couldn't get it working..

$variable = "hey&&what|who;see";

So that $variable is cleaned, so only the following is returned:

heywhatwhosee
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

I could give it a try and make some regexp but I'm sure others here are (much) better at that. So until someone else answers, here's a good start for regexp http://regularexpressions.info/tutorial.html. Once you start to understand them they can be very usefull.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Try something like this:

Code: Select all

$variable = "hey&&what|who;see de"; 

echo $variable . '<br>';

$search = '(\||&|;)';
$replace = '';
$newvar = preg_replace("/($search)/", $replace, $variable);

echo $newvar;
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Re: Hopefully simple question

Post by trukfixer »

Termina wrote:I'd like a function that takes a variable, and removes certain charachters. I looked at explode, but I couldn't get it working..

$variable = "hey&&what|who;see";

So that $variable is cleaned, so only the following is returned:

heywhatwhosee

Code: Select all

$variable = "hey&&what|who;see";
$clean_string = preg_replace("/[^a-zA-Z]/");//replace anything that is *not* an alphabet character
echo $clean_string;
//outputs heywhatwhosee
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Just out of curiosity .. why would you ever want to remove characters from something the user has entered?
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

onion2k wrote:Just out of curiosity .. why would you ever want to remove characters from something the user has entered?
To make the user go nuts :twisted:
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

onion2k wrote:Just out of curiosity .. why would you ever want to remove characters from something the user has entered?
He never said he did. Reread the first post.

(Or did I miss something? 8O )
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

echo preg_replace('/\W+/', '', $string);
EDIT | Just read trukfixers.... use that if you only want letters and no numbers. Note.... my version allows the underscore character too.
Post Reply