Hopefully simple question
Moderator: General Moderators
Hopefully simple question
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
$variable = "hey&&what|who;see";
So that $variable is cleaned, so only the following is returned:
heywhatwhosee
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.
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;- trukfixer
- Forum Contributor
- Posts: 174
- Joined: Fri May 21, 2004 3:14 pm
- Location: Miami, Florida, USA
Re: Hopefully simple question
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- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
echo preg_replace('/\W+/', '', $string);