Code: Select all
the quick "brown fox" jumpedCode: Select all
the quick "brown-fox" jumpedCode: Select all
"[^"]+"|[\s]+Code: Select all
the-quick---jumpedModerator: General Moderators
Code: Select all
the quick "brown fox" jumpedCode: Select all
the quick "brown-fox" jumpedCode: Select all
"[^"]+"|[\s]+Code: Select all
the-quick---jumpedCode: Select all
$text = "The quick \"brown fox\" jumped 'over something' and \"another brown fox\" jumped.";
echo "{$text}\n";
echo preg_replace("/\s+(?!([^'\"]*['\"][^'\"]*['\"])*[^'\"]*$)/", '-', $text);Hmmm, if you're not really comfortable with regex-es, I don't know if you're goingtristanlee85 wrote:Thank you for the reply! I wasn't going to go as far to look into balancing the quotes. I mean, Google doesn't automatically split my words when I type with without a space.
This works just like I was hoping. I'll take you up on your offer for explaining it if you would like. I've read tutorial after tutorial and RegEx is something I can't understand.
Code: Select all
'/\s+(?!([^'\"]*['\"][^'\"]*['\"])*[^'\"]*$)/'
Code: Select all
\s // Mathces a single white space character
X+ // One or more 'X'-s
X* // Zero or more 'X'-s
[XY] // Matches either 'X' or 'Y'
[^XY] // Matches any character except 'X' and 'Y'
X(?!Y) // Match the character 'X' only if there isn't a 'Y' ahead of it (so,
// it matches 'XQ' and 'XC' etc. but does not match 'XY'). This is
// called: 'negative look-ahead'.
$ // Meta character for the 'end of the string'Code: Select all
\s+ // Match one or more white space characters ...
(?! // start negative look-ahead
( // open group 1
[^'\"]* // zero or more characters of any type except single or double quotes
['\"] // one single or double quote
[^'\"]* // zero or more characters of any type except single or double quotes
['\"] // one single or double quote
) // close group 1
* // group 1 can occur zero or more times (in other words, quotes
// can only occur 0, 2, 4, 6, .. times, ie an even number of times)
[^'\"]* // zero or more characters of any type except single or double quotes
$ // the end of the string
) // stop negative look-ahead