Masi Masi - 5 months ago 17
PHP Question

To strip whitespaces inside a variable in PHP

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);

Answer

To strip any whitespace, you can use a regular expression

$str=preg_replace('/\s+/', '', $str);