regex - Removing whitespace-characters, except inside quotation marks in PHP? -


i need remove whitespace string, quotations should stay were.

here's example:

string parse: hola hola "pepsi cola" yay  output: holahola"pepsi cola"yay 

any idea? i'm sure can done regex, solution okay.

we match strings or quotations with

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

so need preg_match_all , concatenate result.


example:

$str = 'hola hola "pepsi cola" yay';  preg_match_all('/[^\s"]+|"[^"]*"/', $str, $matches);  echo implode('', $matches[0]); // holahola"pepsi cola"yay 

Comments

Popular posts from this blog

List<T>().Add problem C# -

Javascript natural sort array/object and maintain index association -