I18N_Arabic
[ class tree: I18N_Arabic ] [ index: I18N_Arabic ] [ all elements ]

Source for file CharsetC.php

Documentation is available at CharsetC.php

  1. <?php
  2. /**
  3.  * ----------------------------------------------------------------------
  4.  *  
  5.  * Copyright (c) 2006-2012 Khaled Al-Sham'aa.
  6.  *  
  7.  * http://www.ar-php.org
  8.  *  
  9.  * PHP Version 5
  10.  *  
  11.  * ----------------------------------------------------------------------
  12.  *  
  13.  * LICENSE
  14.  *
  15.  * This program is open source product; you can redistribute it and/or
  16.  * modify it under the terms of the GNU Lesser General Public License (LGPL)
  17.  * as published by the Free Software Foundation; either version 3
  18.  * of the License, or (at your option) any later version.
  19.  *  
  20.  * This program is distributed in the hope that it will be useful,
  21.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  * GNU Lesser General Public License for more details.
  24.  *  
  25.  * You should have received a copy of the GNU Lesser General Public License
  26.  * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl.txt>.
  27.  *  
  28.  * ----------------------------------------------------------------------
  29.  *  
  30.  * Class Name: Arabic Charset Converter
  31.  *  
  32.  * Filename:   CharsetC.php
  33.  *  
  34.  * Original    Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:    Convert a given Arabic string from one Arabic character set to
  37.  *             another, those available character sets includes the most popular
  38.  *             three: Windows-1256, ISO 8859-6, and UTF-8
  39.  *              
  40.  * ----------------------------------------------------------------------
  41.  *  
  42.  * Arabic Charset Converter
  43.  *
  44.  * PHP class to convert a given Arabic string from one Arabic character set
  45.  * to another, those available character sets includes the most popular three:
  46.  * Windows-1256, ISO 8859-6, and UTF-8.
  47.  *
  48.  * Example:
  49.  * <code>
  50.  *   include('./I18N/Arabic.php');
  51.  *   $obj = new I18N_Arabic('CharsetC');
  52.  *
  53.  *   $obj->setInputCharset('windows-1256');
  54.  *   $obj->setOutputCharset('utf-8');
  55.  *   
  56.  *   $charset = $obj->getOutputCharset();
  57.  *      
  58.  *   $text = $obj->convert($text);
  59.  * </code>
  60.  *
  61.  * @category  I18N
  62.  * @package   I18N_Arabic
  63.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  64.  * @copyright 2006-2012 Khaled Al-Sham'aa
  65.  *    
  66.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  67.  * @link      http://www.ar-php.org
  68.  */
  69.  
  70. // New in PHP V5.3: Namespaces
  71. // namespace I18N\Arabic;
  72. // 
  73. // $obj = new I18N\Arabic\CharsetC();
  74. // 
  75. // use I18N\Arabic;
  76. // $obj = new Arabic\CharsetC();
  77. //
  78. // use I18N\Arabic\CharsetC as CharsetC;
  79. // $obj = new CharsetC();
  80.  
  81. /**
  82.  * This PHP class converts a given Arabic string from
  83.  * one Arabic character set to another
  84.  *  
  85.  * @category  I18N
  86.  * @package   I18N_Arabic
  87.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  88.  * @copyright 2006-2012 Khaled Al-Sham'aa
  89.  *    
  90.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  91.  * @link      http://www.ar-php.org
  92.  */ 
  93. {
  94.     private $_utfStr  '';
  95.     private $_winStr  '';
  96.     private $_isoStr  '';
  97.     private $_htmlStr '';
  98.  
  99.     // Hold an instance of the class
  100.     private static $_instance;
  101.     
  102.     /**
  103.      * Loads initialize values (this should be private method because of singleton)
  104.      * 
  105.      * @param array $sets Charsets you would like to support
  106.      */         
  107.     public function __construct($sets array('windows-1256''utf-8'))
  108.     {
  109.         $handle fopen(dirname(__FILE__).'/data/charset/charset.src''r');
  110.         if ($handle{
  111.             $this->_utfStr  fgets($handle4096);
  112.             $this->_winStr  fgets($handle4096);
  113.             $this->_isoStr  fgets($handle4096);
  114.             $this->_htmlStr fgets($handle4096);
  115.             fclose($handle);
  116.         }
  117.  
  118.         if (in_array('windows-1256'$sets)) {
  119.             include dirname(__FILE__).'/data/charset/_windows1256.php';
  120.         }
  121.         
  122.         if (in_array('iso-8859-6'$sets)) {
  123.             include dirname(__FILE__).'/data/charset/_iso88596.php';
  124.         }
  125.         
  126.         if (in_array('utf-8'$sets)) {
  127.             include dirname(__FILE__).'/data/charset/_utf8.php';
  128.         }
  129.         
  130.         if (in_array('bug'$sets)) {
  131.             include dirname(__FILE__).'/data/charset/_bug.php';
  132.         }
  133.         
  134.         if (in_array('html'$sets)) {
  135.             include dirname(__FILE__).'/data/charset/_html.php';
  136.         }
  137.     }
  138.  
  139.     /**
  140.      * The singleton method
  141.      * 
  142.      * @return object Instance of this class
  143.      * 
  144.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  145.      */ 
  146.     public static function singleton(
  147.     {
  148.         // if (!(self::$_instance instanceof self)) {
  149.         if (!isset(self::$_instance)) {
  150.             $c = __CLASS__;
  151.  
  152.             self::$_instance new $c;
  153.         }
  154.  
  155.         return self::$_instance;
  156.     }
  157.  
  158.     /**
  159.      * Prevent users to clone the instance
  160.      *
  161.      * @return void 
  162.      */
  163.     private function __clone(
  164.     {
  165.         trigger_error('Clone is not allowed.'E_USER_ERROR);
  166.     }
  167.  
  168.     /**
  169.      * Get HTML entity from given position
  170.      *      
  171.      * @param integer $index Extract position
  172.      *      
  173.      * @return string HTML entity
  174.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  175.      */
  176.     protected function getHTML($index)
  177.     {
  178.         return trim(substr($this->_htmlStr$index*44));
  179.     }
  180.     
  181.     /**
  182.      * Get UTF character from given position
  183.      *      
  184.      * @param integer $index Extract position
  185.      *      
  186.      * @return string UTF character
  187.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  188.      */
  189.     protected function getUTF($index)
  190.     {
  191.         return trim(substr($this->_utfStr$index*22));
  192.     }
  193.     
  194.     /**
  195.      * Get extract position of a given UTF character
  196.      *      
  197.      * @param string $char UTF character
  198.      *      
  199.      * @return integer Extract position
  200.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  201.      */
  202.     protected function findUTF($char)
  203.     {
  204.         if (!$char{
  205.             return false;
  206.         }
  207.         return strpos($this->_utfStr$char)/2;
  208.     }
  209.     
  210.     /**
  211.      * Get Windows-1256 character from given position
  212.      *      
  213.      * @param integer $index Extract position
  214.      *      
  215.      * @return string Windows-1256 character
  216.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  217.      */
  218.     protected function getWIN($index)
  219.     {
  220.         return substr($this->_winStr$index1);
  221.     }
  222.     
  223.     /**
  224.      * Get extract position of a given Windows-1256 character
  225.      *      
  226.      * @param string $char Windows-1256 character
  227.      *      
  228.      * @return integer Extract position
  229.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  230.      */
  231.     protected function findWIN($char)
  232.     {
  233.         if (!$char{
  234.             return false;
  235.         }
  236.         return strpos($this->_winStr$char);
  237.     }
  238.     
  239.     /**
  240.      * Get ISO-8859-6 character from given position
  241.      *      
  242.      * @param integer $index Extract position
  243.      *      
  244.      * @return string ISO-8859-6 character
  245.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  246.      */
  247.     protected function getISO($index)
  248.     {
  249.         return substr($this->_isoStr$index1);
  250.     }
  251.     
  252.     /**
  253.      * Get extract position of a given ISO-8859-6 character
  254.      *      
  255.      * @param string $char ISO-8859-6 character
  256.      *      
  257.      * @return integer Extract position
  258.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  259.      */
  260.     protected function findISO($char)
  261.     {
  262.         if (!$char{
  263.             return false;
  264.         }
  265.         return strpos($this->_isoStr$char);
  266.     }
  267.     
  268.     /**
  269.      * Convert Arabic string from Windows-1256 to ISO-8859-6 format
  270.      *      
  271.      * @param string $string Original Arabic string in Windows-1256 format
  272.      *      
  273.      * @return string Converted Arabic string in ISO-8859-6 format
  274.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  275.      */
  276.     public function win2iso($string)
  277.     {
  278.         $chars     preg_split('//'$string);
  279.         $converted null;
  280.         
  281.         foreach ($chars as $char{
  282.             $key $this->findWIN($char);
  283.             if (is_int($key)) {
  284.                 $converted .= $this->getISO($key);
  285.             else {
  286.                 $converted .= $char;
  287.             }
  288.         }
  289.         return $converted;
  290.     }
  291.     
  292.     /**
  293.      * Convert Arabic string from Windows-1256 to UTF-8 format
  294.      *      
  295.      * @param string $string Original Arabic string in Windows-1256 format
  296.      *      
  297.      * @return string Converted Arabic string in Windows-1256 format
  298.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  299.      */
  300.     public function win2utf($string)
  301.     {
  302.         $chars     preg_split('//'$string);
  303.         $converted null;
  304.         
  305.         foreach ($chars as $char{
  306.             $key $this->findWIN($char);
  307.  
  308.             if (is_int($key)) {
  309.                 $converted .= $this->getUTF($key);
  310.             else {
  311.                 $converted .= $char;
  312.             }
  313.         }
  314.         return $converted;
  315.     }
  316.  
  317.     /**
  318.      * Convert Arabic string from Windows-1256 to HTML entities format
  319.      *      
  320.      * @param string $string Original Arabic string in Windows-1256 format
  321.      *      
  322.      * @return string Converted Arabic string in HTML entities format
  323.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  324.      */
  325.     public function win2html($string)
  326.     {
  327.         $chars     preg_split('//'$string);
  328.         $converted null;
  329.         
  330.         foreach ($chars as $char{
  331.             $key $this->findWIN($char);
  332.  
  333.             if (is_int($key&& $key 58{
  334.                 $converted .= '&#' $this->getHTML($key';';
  335.             else {
  336.                 $converted .= $char;
  337.             }
  338.         }
  339.         return $converted;
  340.     }
  341.     
  342.     /**
  343.      * Convert Arabic string from ISO-8859-6 to Windows-1256 format
  344.      *      
  345.      * @param string $string Original Arabic string in ISO-8859-6 format
  346.      *      
  347.      * @return string Converted Arabic string in Windows-1256 format
  348.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  349.      */
  350.     public function iso2win($string)
  351.     {
  352.         $chars     preg_split('//'$string);
  353.         $converted null;
  354.         
  355.         foreach ($chars as $char{
  356.             $key $this->findISO($char);
  357.             if (is_int($key)) {
  358.                 $converted .= $this->getWIN($key);
  359.             else {
  360.                 $converted .= $char;
  361.             }
  362.         }
  363.         return $converted;
  364.     }
  365.     
  366.     /**
  367.      * Convert Arabic string from ISO-8859-6 to UTF-8 format
  368.      *      
  369.      * @param string $string Original Arabic string in ISO-8859-6 format
  370.      *      
  371.      * @return string Converted Arabic string in UTF-8 format
  372.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  373.      */
  374.     public function iso2utf($string)
  375.     {
  376.         $chars     preg_split('//'$string);
  377.         $converted null;
  378.         
  379.         foreach ($chars as $char{
  380.             $key $this->findISO($char);
  381.             if (is_int($key)) {
  382.                 $converted .= $this->getUTF($key);
  383.             else {
  384.                 $converted .= $char;
  385.             }
  386.         }
  387.         return $converted;
  388.     }
  389.     
  390.     /**
  391.      * Convert Arabic string from UTF-8 to Windows-1256 format
  392.      *      
  393.      * @param string $string Original Arabic string in UTF-8 format
  394.      *      
  395.      * @return string Converted Arabic string in Windows-1256 format
  396.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  397.      */
  398.     public function utf2win($string)
  399.     {
  400.         $chars     preg_split('//'$string);
  401.         $converted null;
  402.         
  403.         $cmp false;
  404.         foreach ($chars as $char{
  405.             $ascii ord($char);
  406.             if (($ascii == 216 || $ascii == 217&& !$cmp{
  407.                 $code $char;
  408.                 $cmp  true;
  409.                 continue;
  410.             }
  411.             if ($cmp{
  412.                 $code .= $char;
  413.                 $cmp   false;
  414.                 $key   $this->findUTF($code);
  415.                 if (is_int($key)) {
  416.                     $converted .= $this->getWIN($key);
  417.                 }
  418.             else {
  419.                 $converted .= $char;
  420.             }
  421.         }
  422.         return $converted;
  423.     }
  424.     
  425.     /**
  426.      * Convert Arabic string from UTF-8 to ISO-8859-6 format
  427.      *      
  428.      * @param string $string Original Arabic string in UTF-8 format
  429.      *      
  430.      * @return string Converted Arabic string in ISO-8859-6 format
  431.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  432.      */
  433.     public function utf2iso($string)
  434.     {
  435.         $chars     preg_split('//'$string);
  436.         $converted null;
  437.         
  438.         $cmp false;
  439.         foreach ($chars as $char{
  440.             $ascii ord($char);
  441.             if (($ascii == 216 || $ascii == 217&& !$cmp{
  442.                 $code $char;
  443.                 $cmp  true;
  444.                 continue;
  445.             }
  446.             if ($cmp{
  447.                 $code .= $char;
  448.                 $cmp   false;
  449.                 $key   $this->findUTF($code);
  450.                 if (is_int($key)) {
  451.                     $converted .= $this->getISO($key);
  452.                 }
  453.             else {
  454.                 $converted .= $char;
  455.             }
  456.         }
  457.         return $converted;
  458.     }
  459.     
  460.     /**
  461.      * Convert buggy Arabic imported database string to Windows-1256 format
  462.      *      
  463.      * @param string $string Original corrupted Arabic string, usually when export
  464.      *                        database from MySQL < 4.1 into MySQL >= 4.1
  465.      *                        using phpMyAdmin tool where each Arabic UTF-8
  466.      *                        character translate as two ISO-8859-1 characters in
  467.      *                        export, then translate them into UTF-8 format in import.
  468.      *                    
  469.      * @return string Converted Arabic string in Windows-1256 format
  470.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  471.      */
  472.     public function bug2win($string)
  473.     {
  474.         $chars     preg_split('//'$string);
  475.         $converted null;
  476.         
  477.         $cmp false;
  478.         foreach ($chars as $char{
  479.             $ascii ord($char);
  480.             if (($ascii == 195 || $ascii == 194&& !$cmp{
  481.                 $code $char;
  482.                 $cmp  true;
  483.                 continue;
  484.             }
  485.             if ($cmp{
  486.                 $code .= $char;
  487.                 $cmp   false;
  488.                 $key   array_search($code$this->bug);
  489.                 if (is_int($key)) {
  490.                     $converted .= $this->getWIN($key);
  491.                 }
  492.             else {
  493.                 $converted .= $char;
  494.             }
  495.         }
  496.         return $converted;
  497.     }
  498.     
  499.     /**
  500.      * Convert buggy Arabic imported database string to UTF-8 format
  501.      *      
  502.      * @param string $string Original corrupted Arabic string, usually when export
  503.      *                        database from MySQL < 4.1 into MySQL >= 4.1 using
  504.      *                        phpMyAdmin tool where each Arabic UTF-8 character
  505.      *                        translate as two ISO-8859-1 characters in export,
  506.      *                        then translate them into UTF-8 format in import.
  507.      *                    
  508.      * @return string Converted Arabic string in UTF-8 format
  509.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  510.      */
  511.     public function bug2utf($string)
  512.     {
  513.         $chars     preg_split('//'$string);
  514.         $converted null;
  515.         
  516.         $cmp false;
  517.         foreach ($chars as $char{
  518.             $ascii ord($char);
  519.             if (($ascii == 195 || $ascii == 194&& !$cmp{
  520.                 $code $char;
  521.                 $cmp  true;
  522.                 continue;
  523.             }
  524.             if ($cmp{
  525.                 $code .= $char;
  526.                 $cmp   false;
  527.                 $key   array_search($code$this->bug);
  528.                 if (is_int($key)) {
  529.                     $converted .= $this->getUTF($key);
  530.                 }
  531.             else {
  532.                 $converted .= $char;
  533.             }
  534.         }
  535.         return $converted;
  536.     }
  537.     
  538.     /**
  539.      * Convert buggy Arabic imported database string to ISO-8859-6 format
  540.      *      
  541.      * @param string $string Original corrupted Arabic string, usually when export
  542.      *                        database from MySQL < 4.1 into MySQL >= 4.1 using
  543.      *                        phpMyAdmin tool where each Arabic UTF-8 character
  544.      *                        translate as two ISO-8859-1 characters in export,
  545.      *                        then translate them into UTF-8 format in import.
  546.      *                    
  547.      * @return string Converted Arabic string in ISO-8859-6 format
  548.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  549.      */
  550.     public function bug2iso($string)
  551.     {
  552.         $chars     preg_split('//'$string);
  553.         $converted null;
  554.         
  555.         $cmp false;
  556.         foreach ($chars as $char{
  557.             $ascii ord($char);
  558.             if (($ascii == 195 || $ascii == 194&& !$cmp{
  559.                 $code $char;
  560.                 $cmp  true;
  561.                 continue;
  562.             }
  563.             if ($cmp{
  564.                 $code .= $char;
  565.                 $cmp   false;
  566.                 $key   array_search($code$this->bug);
  567.                 if (is_int($key)) {
  568.                     $converted .= $this->getISO($key);
  569.                 }
  570.             else {
  571.                 $converted .= $char;
  572.             }
  573.         }
  574.         return $converted;
  575.     }
  576.     
  577.     /**
  578.      * Convert buggy Arabic string as HTML entities to UTF-8 format
  579.      *      
  580.      * @param string $string Original corrupted Arabic string, usually when insert
  581.      *                        Arabic string as HTML entities.
  582.      *                            
  583.      * @return string Converted Arabic string in UTF-8 format
  584.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  585.      */
  586.     public function html2utf($string)
  587.     {
  588.         $converted preg_replace($this->html$this->utf8$string);
  589.         return $converted;
  590.     }
  591.     
  592.     /**
  593.      * Convert buggy Arabic string as HTML entities to Windows-1256 format
  594.      *                    
  595.      * @param string $string Original corrupted Arabic string, usually when insert
  596.      *                        Arabic string as HTML entities.
  597.      *                    
  598.      * @return string Converted Arabic string in Windows-1256 format
  599.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  600.      */
  601.     public function html2win($string)
  602.     {
  603.         $converted preg_replace($this->html$this->windows1256$string);
  604.         return $converted;
  605.     }
  606.     
  607.     /**
  608.      * Convert buggy Arabic string as HTML entities to ISO-8859-6 format
  609.      *      
  610.      * @param string $string Original corrupted Arabic string, usually when insert
  611.      *                        Arabic string as HTML entities.
  612.      *                    
  613.      * @return string Converted Arabic string in ISO-8859-6 format
  614.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  615.      */
  616.     public function html2iso($string)
  617.     {
  618.         $converted preg_replace($this->html$this->iso88596$string);
  619.         return $converted;
  620.     }
  621. }

Documentation generated on Tue, 17 Jan 2012 09:18:10 +0200 by phpDocumentor 1.4.0