Click to See Complete Forum and Search --> : [RESOLVED] removing all whitespace from a string


cinematic_jesi
02-22-2008, 07:04 PM
Hi--

I know how to replace whitespace at the beginning and end of a string, but I'm not to sure about within? If that makes sense..

I currently have this function:

mkdir("../../gallery/images/" . strtolower($_POST['path']), 0700);
I need it to be able to if $POST['path'] = new folder it would convert it to newfolder. I have to also have the strtolower incorporated because everything has to be lowercase..

Should I be using something like regex? I heard that's not a good, solid method to use though...

Thanks for any help!

NogDog
02-22-2008, 07:28 PM
There is nothing wrong with the preg_* regex functions when needed. However, if all you need to replace are spaces, str_replace() should work fine:

$newString = str_replace(' ', '', $string);

If you want to replace anything that is not a letter, number or underscore:

$newString = preg_replace('/\W/', '', $string);

iota
02-22-2008, 11:50 PM
why not use trim() ?
It's only simple solution for removing white spaces of begin and end of $REQUEST

Dragonkai
02-23-2008, 02:15 AM
Why you answered your own question.

"Why not trim()"

Well cause it "only" does the begin and end. While preg_replace or str_replace does it all.

cinematic_jesi
02-23-2008, 11:08 AM
Thanks for your guys' help!