// "" => "[^\""]*(\.[^\"]*)*"
$text1 = " April fools 'a b'c d' day is "a b\"c d" 04/01/2002 ";
$text2 = " April fools 'a d' day is "a d" 04/01/2002 ";
function reduce_space($matches)
{
// as usual: $matches[0] is the complete match
// $matches[1] the match for the first subpattern
// enclosed in '(...)' and so on
return $matches[1].' '.$matches[3];
}
$pattern = '|([^ '"]*)( +)(('[^\\']*(\\.[^\\']*)*')*("[^\\"]*(\\.[^\\"]*)*")*)|';
echo preg_replace_callback($pattern, "reduce_space", $text1);
echo "done1n";
echo preg_replace_callback($pattern, "reduce_space", $text2);
echo "done2n";
查看源码吧,显示时可能多个空格显示为一个空格了。