Change Mail::_encodeHeaderValue() to use regexp instead of a for() loop
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 7 Jul 2007 23:50:46 +0000 (23:50 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 7 Jul 2007 23:50:46 +0000 (23:50 +0000)
mail.php

index a97b62403eefb7ef21f1dbacf6316edaf8ca4330..e78cb4158d713bd5cd4784a5e119c13f0d8d4c21 100644 (file)
--- a/mail.php
+++ b/mail.php
@@ -354,24 +354,12 @@ class Mail
        */
        function _encodeHeaderValue($text)
        {
-               $enc = '';
+               // perform this on non-ASCII characters; excluding _ and = because we want them to be encoded as they have
+               // different meanings in mail messages
+               $text = preg_replace('#([^a-zA-Z0-9\+\-\*!/])#e', '"=" . strtoupper(dechex(ord("\\1")))', $text);
+               $text = str_replace('=20', '_' , $text);
                
-               for ($i = 0; $i < strlen($text); $i++)
-               {
-                       $char = ord($text[$i]);
-                       // allowed characters: a-zA-Z0-9_\+\-\*_=
-                       if (($char >= 65 AND $char <= 90) OR ($char >= 97 AND $char <= 122) OR ($char >= 48 AND $char <= 57) OR $char == 33 OR $char == 42 OR $char == 43 OR $char == 45 OR $char == 47 OR $char == 61)
-                       {
-                               $enc .= $text[$i];
-                       }
-                       else
-                       {
-                               // this is a non-ASCII character, so encode it
-                               $enc .= '=' . strtoupper(dechex($char));
-                       }
-               }
-               
-               return '=?' . $this->charset . '?q?' . $enc . '?=';
+               return '=?' . $this->charset . '?q?' . $text . '?=';
        }
 }