Re: How to get WIN-1255 encoded string

Fredrik Tolf <[email protected]>
Newsgroups gmane.comp.php.internationalization,gmane.comp.php.general
Message-ID <[email protected]>
On Sun, 2004-11-07 at 13:57 +0200, Marina Markus wrote:
> Hello,

Hi!
 
> I need a PHP script to create an email message in a way that a subject line
> in Hebrew will be readable in all mail clients. Some mail clients cannot
> cope with Hebrew if they don't have character set explicitly denoted. 

I believe that should be _all_ e-mail clients. Since the default MIME
charset is US-ASCII (as per RFC 2047 and 2822), only faulty e-mail
clients would be able to cope with non-ASCII characters without wrapping
the header in MIME.
 
> Is there a possibility in PHP allowing to encode a string
> with WIN-1255 character set encoding? The result should look like:
>  
> =?windows-1255?B?Rlc6IOz26eHl+CDk8uXj6e0=?=
>  
> which the mail client then is able to decode back into Hebrew. 

If you're running PHP5, you might want to look at iconv_mime_encode. If
not, you can take these functions, which are from a GPL:d webmail I'm
writing, and adapt them to your purpose:

function mimifyhdr($header)
{
    if(strpos($header, " ") !== FALSE)
    {
	$temp = "";
	$cp = 0;
	while(($np = strpos($header, " ", $cp)) !== FALSE)
	{
	    $temp .= mimifyhdr(substr($header, $cp, $np - $cp)) . " ";
	    $cp = $np + 1;
	}
	$temp .= mimifyhdr(substr($header, $cp));
	return($temp);
    }
    $nh = "";
    $num = 0;
    for($i = 0; $i < strlen($header); $i++)
    {
	$c = substr($header, $i, 1);
	if(ord($c) >= 128)
	    $num++;
    }
    if($num == 0)
	return($header);
    if($num > (strlen($header) / 4))
	return("=?UTF-8?B?" . encodemime($header, "base64") . "?=");
    $nt = "";
    for($i = 0; $i < strlen($header); $i++)
    {
	$c = substr($header, $i, 1);
	if(($c == "=") || (ord($c) >= 128) || ($c == "_"))
	{
	    $nt .= "=" . strtoupper(dechex(ord($c)));
	} else if($c == " ") {
	    $nt .= "_";
	} else {
	    $nt .= $c;
	}
    }
    return("=?UTF-8?Q?" . $nt . "?=");
}

function addhdr($name, $val)
{
    global $headers;
    
    if(trim($val) != "")
    {
	$temp .= $name . ": " . mimifyhdr($val);
	$maxlen = strlen($temp);
	$ls = 0;
	while($maxlen > 70)
	{
	    $cp = $ls + 70;
	    while(strpos("\t ", substr($temp, $cp, 1)) === FALSE)
	    {
		if(--$cp < $ls)
		    break;
	    }
	    if($cp < $ls)
		break;
	    $temp = substr($temp, 0, $cp) . "\r\n\t" .
		    substr($temp, $cp+ 1);
	    $ls = $cp + 3;
	    $maxlen = strlen($temp) - $ls;
	}
	$headers .= $temp . "\r\n";
    }
}

Note that they take values which are already in the UTF-8 encoding. If
your values aren't in UTF-8, take a look at the iconv function.

> If anyone has a solution for another character set, I suppose it can also
> help.

I'd recommend that you use UTF-8 instead. It's more general than
Windows-1255 (copes with characters outside the Hebrew range), and is
probably supported by more MUAs.

Hope it helps!

Fredrik Tolf

-- 
PHP Internationalization Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.