tags:

views:

494

answers:

3

How can you strip whitespaces in PHP's variable?

I know this comment PHP.net. I would like to have a similar tool like tr for PHP such that I can run simply

tr -d " " ""

I run unsuccessfully the function php_strip_whitespace by

$tags_trimmed = php_strip_whitespace($tags);

I run the regex function also unsuccessfully

$tags_trimmed = preg_replace(" ", "", $tags);
+3  A: 
$string = str_replace(" ", "", $string);

I believe preg_replace would be looking for something like [:space:]

Chacha102
+2  A: 

If you want to remove all whitespaces everywhere from $tags why not just:

str_replace(' ', '', $tags);

If you want to remove new lines and such that would require a bit more...

camomileCase
if you don't assign the result to a variable, this would not actually do anything useful.
Paul Dixon
+8  A: 

To strip any whitespace, you can use a regular expression

$str=preg_replace('/\s+/', '', $str);
Paul Dixon
+1 because this removes all whitespace and not just spaces.
MitMaro