troubleshooting Question

Regular expression (Perl) / Preg_replace. Replace all spaces except those inside quotes ???

Avatar of R-U-Bn
R-U-Bn asked on
PHPRegular Expressions
6 Comments1 Solution5955 ViewsLast Modified:
Hello, I want my preg_replace change all (multiple) spaces into one charachter | , EXCEPT those spaces that are inside quotes, but I can't find the right expression.  

Some examples:
testA testB testC testD            -->      testA|testB|testC|testD
testA "testB testC" testD      -->      testA|"testB testC"|testD
testA "testB testC testD"      -->      testA|"testB testC testD"
"testA testB testC" testD      -->      "testA testB testC"|testD

If possible also with the quotes gone, but I can do that with placing the quote in a second item of 2 arrays in preg_replace.

What I found so far is the following, but it's no good:
preg_replace ( array('/(?<![" ])\b(\s+)\b/','/\\\"/'), array('|','')

Help, I need somebody.  Help, not just anybody.  He-e-elp.  (Referring to the song ;-) )
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros