- // need quoted-string? find special chars: SP, CTL, (, ), {, %, *, ", \, ]
- // plus [ character as a workaround for DBMail's bug (#1487766)
- else if ($force_quotes ||
- preg_match('/([\x00-\x20\x28-\x29\x7B\x25\x2A\x22\x5B\x5C\x5D\x7F]+)/', $string)
- ) {
- return '"' . strtr($string, array('"'=>'\\"', '\\' => '\\\\')) . '"';
+ // atom-string (only safe characters)
+ if (!$force_quotes && !preg_match('/[\x00-\x20\x22\x28-\x2A\x5B-\x5D\x7B\x7D\x80-\xFF]/', $string)) {
+ return $string;
+ }
+ // quoted-string
+ if (!preg_match('/[\r\n\x00\x80-\xFF]/', $string)) {
+ return '"' . addcslashes($string, '\\"') . '"';