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

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -