views:

19

answers:

1

Hey

I couldn't find any similar question, so I start a new one.

I need to remove all whitespace from string, but quotations should stay as they were. I'm building a personal kind of minifier.

Here's an example:

string to parse:
hola hola "pepsi cola" yay

output:
holahola"pepsi cola"yay

Any idea? I'm sure this can be done with regexp, but any solution is okay.

Martti Laine

+1  A: 

We could match strings or quotations with

[^\s"]+|"[^"]*"

So we just need to preg_match_all and concatenate the result.


Example:

$str = 'hola hola "pepsi cola" yay';

preg_match_all('/[^\s"]+|"[^"]*"/', $str, $matches);

echo implode('', $matches[0]);
// holahola"pepsi cola"yay
KennyTM