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-2013 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-2013 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-2013 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 HTML entities format
  344.      *      
  345.      * @param string $string Original Arabic string in ISO-8859-6 format
  346.      *      
  347.      * @return string Converted Arabic string in HTML entities format
  348.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  349.      */
  350.     public function iso2html($string)
  351.     {
  352.         $chars     preg_split('//'$string);
  353.         $converted null;
  354.         
  355.         foreach ($chars as $char{
  356.             $key $this->findISO($char);
  357.  
  358.             if (is_int($key&& $key 58{
  359.                 $converted .= '&#' $this->getHTML($key';';
  360.             else {
  361.                 $converted .= $char;
  362.             }
  363.         }
  364.         return $converted;
  365.     }
  366.  
  367.     /**
  368.      * Convert Arabic string from UTF-8 to HTML entities format
  369.      *      
  370.      * @param string $string Original Arabic string in UTF-8 format
  371.      *      
  372.      * @return string Converted Arabic string in HTML entities format
  373.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  374.      */
  375.     public function utf2html($string)
  376.     {
  377.         $chars     preg_split('//'$string);
  378.         $converted null;
  379.         
  380.         $cmp false;
  381.         foreach ($chars as $char{
  382.             $ascii ord($char);
  383.             if (($ascii == 216 || $ascii == 217&& !$cmp{
  384.                 $code $char;
  385.                 $cmp  true;
  386.                 continue;
  387.             }
  388.             if ($cmp{
  389.                 $code .= $char;
  390.                 $cmp   false;
  391.                 $key   $this->findUTF($code);
  392.                 if (is_int($key&& $key 58{
  393.                     $converted .= '&#' $this->getHTML($key';';
  394.                 }
  395.             else {
  396.                 $converted .= $char;
  397.             }
  398.         }
  399.         return $converted;
  400.     }
  401.     
  402.     /**
  403.      * Convert Arabic string from ISO-8859-6 to Windows-1256 format
  404.      *      
  405.      * @param string $string Original Arabic string in ISO-8859-6 format
  406.      *      
  407.      * @return string Converted Arabic string in Windows-1256 format
  408.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  409.      */
  410.     public function iso2win($string)
  411.     {
  412.         $chars     preg_split('//'$string);
  413.         $converted null;
  414.         
  415.         foreach ($chars as $char{
  416.             $key $this->findISO($char);
  417.             if (is_int($key)) {
  418.                 $converted .= $this->getWIN($key);
  419.             else {
  420.                 $converted .= $char;
  421.             }
  422.         }
  423.         return $converted;
  424.     }
  425.     
  426.     /**
  427.      * Convert Arabic string from ISO-8859-6 to UTF-8 format
  428.      *      
  429.      * @param string $string Original Arabic string in ISO-8859-6 format
  430.      *      
  431.      * @return string Converted Arabic string in UTF-8 format
  432.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  433.      */
  434.     public function iso2utf($string)
  435.     {
  436.         $chars     preg_split('//'$string);
  437.         $converted null;
  438.         
  439.         foreach ($chars as $char{
  440.             $key $this->findISO($char);
  441.             if (is_int($key)) {
  442.                 $converted .= $this->getUTF($key);
  443.             else {
  444.                 $converted .= $char;
  445.             }
  446.         }
  447.         return $converted;
  448.     }
  449.     
  450.     /**
  451.      * Convert Arabic string from UTF-8 to Windows-1256 format
  452.      *      
  453.      * @param string $string Original Arabic string in UTF-8 format
  454.      *      
  455.      * @return string Converted Arabic string in Windows-1256 format
  456.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  457.      */
  458.     public function utf2win($string)
  459.     {
  460.         $chars     preg_split('//'$string);
  461.         $converted null;
  462.         
  463.         $cmp false;
  464.         foreach ($chars as $char{
  465.             $ascii ord($char);
  466.             if (($ascii == 216 || $ascii == 217&& !$cmp{
  467.                 $code $char;
  468.                 $cmp  true;
  469.                 continue;
  470.             }
  471.             if ($cmp{
  472.                 $code .= $char;
  473.                 $cmp   false;
  474.                 $key   $this->findUTF($code);
  475.                 if (is_int($key)) {
  476.                     $converted .= $this->getWIN($key);
  477.                 }
  478.             else {
  479.                 $converted .= $char;
  480.             }
  481.         }
  482.         return $converted;
  483.     }
  484.     
  485.     /**
  486.      * Convert Arabic string from UTF-8 to ISO-8859-6 format
  487.      *      
  488.      * @param string $string Original Arabic string in UTF-8 format
  489.      *      
  490.      * @return string Converted Arabic string in ISO-8859-6 format
  491.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  492.      */
  493.     public function utf2iso($string)
  494.     {
  495.         $chars     preg_split('//'$string);
  496.         $converted null;
  497.         
  498.         $cmp false;
  499.         foreach ($chars as $char{
  500.             $ascii ord($char);
  501.             if (($ascii == 216 || $ascii == 217&& !$cmp{
  502.                 $code $char;
  503.                 $cmp  true;
  504.                 continue;
  505.             }
  506.             if ($cmp{
  507.                 $code .= $char;
  508.                 $cmp   false;
  509.                 $key   $this->findUTF($code);
  510.                 if (is_int($key)) {
  511.                     $converted .= $this->getISO($key);
  512.                 }
  513.             else {
  514.                 $converted .= $char;
  515.             }
  516.         }
  517.         return $converted;
  518.     }
  519.     
  520.     /**
  521.      * Convert buggy Arabic imported database string to Windows-1256 format
  522.      *      
  523.      * @param string $string Original corrupted Arabic string, usually when export
  524.      *                        database from MySQL < 4.1 into MySQL >= 4.1
  525.      *                        using phpMyAdmin tool where each Arabic UTF-8
  526.      *                        character translate as two ISO-8859-1 characters in
  527.      *                        export, then translate them into UTF-8 format in import.
  528.      *                    
  529.      * @return string Converted Arabic string in Windows-1256 format
  530.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  531.      */
  532.     public function bug2win($string)
  533.     {
  534.         $chars     preg_split('//'$string);
  535.         $converted null;
  536.         
  537.         $cmp false;
  538.         foreach ($chars as $char{
  539.             $ascii ord($char);
  540.             if (($ascii == 195 || $ascii == 194&& !$cmp{
  541.                 $code $char;
  542.                 $cmp  true;
  543.                 continue;
  544.             }
  545.             if ($cmp{
  546.                 $code .= $char;
  547.                 $cmp   false;
  548.                 $key   array_search($code$this->bug);
  549.                 if (is_int($key)) {
  550.                     $converted .= $this->getWIN($key);
  551.                 }
  552.             else {
  553.                 $converted .= $char;
  554.             }
  555.         }
  556.         return $converted;
  557.     }
  558.     
  559.     /**
  560.      * Convert buggy Arabic imported database string to UTF-8 format
  561.      *      
  562.      * @param string $string Original corrupted Arabic string, usually when export
  563.      *                        database from MySQL < 4.1 into MySQL >= 4.1 using
  564.      *                        phpMyAdmin tool where each Arabic UTF-8 character
  565.      *                        translate as two ISO-8859-1 characters in export,
  566.      *                        then translate them into UTF-8 format in import.
  567.      *                    
  568.      * @return string Converted Arabic string in UTF-8 format
  569.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  570.      */
  571.     public function bug2utf($string)
  572.     {
  573.         $chars     preg_split('//'$string);
  574.         $converted null;
  575.         
  576.         $cmp false;
  577.         foreach ($chars as $char{
  578.             $ascii ord($char);
  579.             if (($ascii == 195 || $ascii == 194&& !$cmp{
  580.                 $code $char;
  581.                 $cmp  true;
  582.                 continue;
  583.             }
  584.             if ($cmp{
  585.                 $code .= $char;
  586.                 $cmp   false;
  587.                 $key   array_search($code$this->bug);
  588.                 if (is_int($key)) {
  589.                     $converted .= $this->getUTF($key);
  590.                 }
  591.             else {
  592.                 $converted .= $char;
  593.             }
  594.         }
  595.         return $converted;
  596.     }
  597.     
  598.     /**
  599.      * Convert buggy Arabic imported database string to ISO-8859-6 format
  600.      *      
  601.      * @param string $string Original corrupted Arabic string, usually when export
  602.      *                        database from MySQL < 4.1 into MySQL >= 4.1 using
  603.      *                        phpMyAdmin tool where each Arabic UTF-8 character
  604.      *                        translate as two ISO-8859-1 characters in export,
  605.      *                        then translate them into UTF-8 format in import.
  606.      *                    
  607.      * @return string Converted Arabic string in ISO-8859-6 format
  608.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  609.      */
  610.     public function bug2iso($string)
  611.     {
  612.         $chars     preg_split('//'$string);
  613.         $converted null;
  614.         
  615.         $cmp false;
  616.         foreach ($chars as $char{
  617.             $ascii ord($char);
  618.             if (($ascii == 195 || $ascii == 194&& !$cmp{
  619.                 $code $char;
  620.                 $cmp  true;
  621.                 continue;
  622.             }
  623.             if ($cmp{
  624.                 $code .= $char;
  625.                 $cmp   false;
  626.                 $key   array_search($code$this->bug);
  627.                 if (is_int($key)) {
  628.                     $converted .= $this->getISO($key);
  629.                 }
  630.             else {
  631.                 $converted .= $char;
  632.             }
  633.         }
  634.         return $converted;
  635.     }
  636.     
  637.     /**
  638.      * Convert buggy Arabic string as HTML entities to UTF-8 format
  639.      *      
  640.      * @param string $string Original corrupted Arabic string, usually when insert
  641.      *                        Arabic string as HTML entities.
  642.      *                            
  643.      * @return string Converted Arabic string in UTF-8 format
  644.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  645.      */
  646.     public function html2utf($string)
  647.     {
  648.         $converted preg_replace($this->html$this->utf8$string);
  649.         return $converted;
  650.     }
  651.     
  652.     /**
  653.      * Convert buggy Arabic string as HTML entities to Windows-1256 format
  654.      *                    
  655.      * @param string $string Original corrupted Arabic string, usually when insert
  656.      *                        Arabic string as HTML entities.
  657.      *                    
  658.      * @return string Converted Arabic string in Windows-1256 format
  659.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  660.      */
  661.     public function html2win($string)
  662.     {
  663.         $converted preg_replace($this->html$this->windows1256$string);
  664.         return $converted;
  665.     }
  666.     
  667.     /**
  668.      * Convert buggy Arabic string as HTML entities to ISO-8859-6 format
  669.      *      
  670.      * @param string $string Original corrupted Arabic string, usually when insert
  671.      *                        Arabic string as HTML entities.
  672.      *                    
  673.      * @return string Converted Arabic string in ISO-8859-6 format
  674.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  675.      */
  676.     public function html2iso($string)
  677.     {
  678.         $converted preg_replace($this->html$this->iso88596$string);
  679.         return $converted;
  680.     }
  681. }

Documentation generated on Mon, 14 Jan 2013 17:48:40 +0100 by phpDocumentor 1.4.0