Regex - Modifying strings except in specified enclosures - PHP

Posted by Kovo on Stack Overflow See other posts from Stack Overflow or by Kovo
Published on 2012-04-09T22:51:32Z Indexed on 2012/04/09 23:30 UTC
Read the original article Hit count: 314

Filed under:
|

I have found similar answers to my current needs on SO, however I am still trying to grasp modifying strings based on a rule, except in certain enclosures within those strings.

Example of what Im trying to accomplish now:

preg_replace("/\s*,\s*/", ",", $text)

I found the above in many places. It will remove spaces before and after all commas in a string.

That works great. However, if I want to exclude modifying commas found within " ", I am not sure how that rule has to be modified.

Any help? Thanks!

EDIT: I want to clarify my question: I would like all whitespace before and after the commas in the following sentence removed, except commas found in double or single quotes:

a, b , c "d, e f g , " , h i j ,k lm,nop

Expected result:

a,b,c "d, e f g , ",h i j,k lm,nop

© Stack Overflow or respective owner

Related posts about php

Related posts about regex