Skip to content
Snippets Groups Projects
Commit 542071af authored by Peter Rotich's avatar Peter Rotich
Browse files

Move encoding to Format class - to be used system wide.

parent 1e359e55
Branches
Tags
No related merge requests found
...@@ -46,6 +46,31 @@ class Format { ...@@ -46,6 +46,31 @@ class Format {
return $result?array_filter($result):$files; return $result?array_filter($result):$files;
} }
/* encode text into desired encoding - taking into accout charset when available. */
function encode($text, $charset=null, $encoding='utf-8') {
//Try auto-detecting charset/encoding
if(!$charset && function_exists('mb_detect_encoding'))
$charset = mb_detect_encoding($text);
//Cleanup - junk
if($charset && in_array(trim($charset), array('default','x-user-defined')))
$charset = 'ASCII';
if(function_exists('iconv') && $charset)
return iconv($charset, $encoding.'//IGNORE', $text);
elseif(function_exists('iconv_mime_decode'))
return iconv_mime_decode($text, 0, $encoding);
else //default to utf8 encoding.
return utf8_encode($text);
}
//Wrapper for utf-8 encoding.
function utf8encode($text, $charset=null) {
return Format::enecode($text, $charset, 'utf-8');
}
function phone($phone) { function phone($phone) {
$stripped= preg_replace("/[^0-9]/", "", $phone); $stripped= preg_replace("/[^0-9]/", "", $phone);
......
...@@ -194,30 +194,17 @@ class MailFetcher { ...@@ -194,30 +194,17 @@ class MailFetcher {
} }
//Convert text to desired encoding..defaults to utf8 //Convert text to desired encoding..defaults to utf8
function mime_encode($text, $charset=null, $enc='utf-8') { //Thank in part to afterburner function mime_encode($text, $charset=null, $encoding='utf-8') { //Thank in part to afterburner
return Format::encode($text, $charset, $encoding);
if($charset && in_array(trim($charset), array('default','x-user-defined')))
$charset = 'ASCII';
if(function_exists('iconv') and ($charset or function_exists('mb_detect_encoding'))) {
if($charset)
return iconv($charset, $enc.'//IGNORE', $text);
elseif(function_exists('mb_detect_encoding'))
return iconv(mb_detect_encoding($text, $this->encodings), $enc, $text);
} elseif(function_exists('iconv_mime_decode')) {
return iconv_mime_decode($text, 0, $enc);
}
return utf8_encode($text);
} }
//Generic decoder - resulting text is utf8 encoded -> mirrors imap_utf8 //Generic decoder - resulting text is utf8 encoded -> mirrors imap_utf8
function mime_decode($text, $enc='utf-8') { function mime_decode($text, $encoding='utf-8') {
$str = ''; $str = '';
$parts = imap_mime_header_decode($text); $parts = imap_mime_header_decode($text);
foreach ($parts as $part) foreach ($parts as $part)
$str.= $this->mime_encode($part->text, $part->charset, $enc); $str.= $this->mime_encode($part->text, $part->charset, $encoding);
return $str?$str:imap_utf8($text); return $str?$str:imap_utf8($text);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment