1<?php 2// $Header: /cvsroot/html2ps/converter.class.php,v 1.6 2006/06/25 13:55:35 Konstantin Exp $ 3 4class Converter { 5 function create() { 6// if (function_exists('iconv')) { 7// return new IconvConverter; 8// } else { 9 return new PurePHPConverter; 10// } 11 } 12} 13 14class IconvConverter { 15 function to_utf8($string, $encoding) { 16 return iconv(strtoupper($encoding), "UTF-8", $string); 17 } 18} 19 20class PurePHPConverter { 21 function apply_aliases($encoding) { 22 global $g_encoding_aliases; 23 24 if (isset($g_encoding_aliases[$encoding])) { 25 return $g_encoding_aliases[$encoding]; 26 } 27 28 return $encoding; 29 } 30 31 function to_utf8($html, $encoding) { 32 global $g_utf8_converters; 33 34 $encoding = $this->apply_aliases($encoding); 35 36 if ($encoding === 'iso-8859-1') { 37 return utf8_encode($html); 38 } elseif ($encoding === 'utf-8') { 39 return $html; 40 } elseif(isset($g_utf8_converters[$encoding])) { 41 return $this->something_to_utf8($html, $g_utf8_converters[$encoding][0]); 42 } else { 43 die("Unsupported encoding detected: '$encoding'"); 44 }; 45 } 46 47 function something_to_utf8($html, &$mapping) { 48 for ($i=0; $i < strlen($html); $i++) { 49 $replacement = code_to_utf8($mapping[$html{$i}]); 50 if ($replacement != $html{$i}) { 51 $html = substr_replace($html, $replacement, $i, 1); 52 $i += strlen($replacement) - 1; 53 }; 54 }; 55 return $html; 56 } 57} 58?>