Page 1 of 1

Hopefully simple question

Posted: Sat Dec 17, 2005 5:45 pm
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

Posted: Sat Dec 17, 2005 6:04 pm
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.

Posted: Sat Dec 17, 2005 6:27 pm
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;

Re: Hopefully simple question

Posted: Sat Dec 17, 2005 7:35 pm
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

Posted: Sun Dec 18, 2005 4:44 am
by onion2k
Just out of curiosity .. why would you ever want to remove characters from something the user has entered?

Posted: Sun Dec 18, 2005 7:23 am
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:

Posted: Sun Dec 18, 2005 8:45 am
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 )

Posted: Sun Dec 18, 2005 12:15 pm
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.