Remove spaces

Need a function to remove spaces in php, but not Trim. Because it should remove spaces not only at the edges, but also in the body of the text.
Those. like this, before deletion:

Audi, bmw em [ Q7 ]

After removing spaces:

Audi,bmw em[Q7]

I.e. it should remove spaces only within brackets []and after a comma, otherwise leave spaces.


Answer 1, authority 100%

$str = preg_replace("/\s*([,\[\]])\s*/", "$1", "Audi, bmw em [ Q7 ]");

or

$str = preg_replace("/\s*([,\{\}])\s*/", "$1", "Audi, bmw em [ Q7 ]");

Answer 2, authority 33%

The function str_replace

will help you

$str = str_replace(" ", "", "Audi [ Q7 ]");

Answer 3, authority 33%

$string = 'Audi, bmw em [ Q7 ]';
$newString = str_replace(array(', ', '[ ', ' ]', ' ['), '', $string);