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

Source for file CharsetD.php

Documentation is available at CharsetD.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: Detect Arabic String Character Set
  31.  *  
  32.  * Filename:   CharsetD.php
  33.  *  
  34.  * Original    Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:    This class will return Arabic character set that used for
  37.  *             a given Arabic string passing into this class, those available
  38.  *             character sets that can be detected by this class includes
  39.  *             the most popular three: Windows-1256, ISO 8859-6, and UTF-8.
  40.  *              
  41.  * ----------------------------------------------------------------------
  42.  *  
  43.  * Detect Arabic String Character Set
  44.  *
  45.  * The last step of the Information Retrieval process is to display the found
  46.  * documents to the user. However, some difficulties might occur at that point.
  47.  * English texts are usually written in the ASCII standard. Unlike the English
  48.  * language, many languages have different character sets, and do not have one
  49.  * standard. This plurality of standards causes problems, especially in a web
  50.  * environment.
  51.  *
  52.  * This PHP class will return Arabic character set that used for a given
  53.  * Arabic string passing into this class, those available character sets that can
  54.  * be detected by this class includes the most popular three: Windows-1256,
  55.  * ISO 8859-6, and UTF-8.
  56.  *
  57.  * Example:
  58.  * <code>
  59.  *   include('./I18N/Arabic.php');
  60.  *   $obj = new I18N_Arabic('CharsetD');
  61.  * 
  62.  *   $charset = $obj->getCharset($text);
  63.  * </code>
  64.  *                
  65.  * @category  I18N
  66.  * @package   I18N_Arabic
  67.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  68.  * @copyright 2006-2013 Khaled Al-Sham'aa
  69.  *    
  70.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  71.  * @link      http://www.ar-php.org
  72.  */
  73.  
  74. // New in PHP V5.3: Namespaces
  75. // namespace I18N\Arabic;
  76. // 
  77. // $obj = new I18N\Arabic\CharsetD();
  78. // 
  79. // use I18N\Arabic;
  80. // $obj = new Arabic\CharsetD();
  81. //
  82. // use I18N\Arabic\CharsetD as CharsetD;
  83. // $obj = new CharsetD();
  84.  
  85. /**
  86.  * This PHP class detect Arabic string character set
  87.  *  
  88.  * @category  I18N
  89.  * @package   I18N_Arabic
  90.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  91.  * @copyright 2006-2013 Khaled Al-Sham'aa
  92.  *    
  93.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  94.  * @link      http://www.ar-php.org
  95.  */ 
  96. {
  97.     /**
  98.      * Loads initialize values
  99.      *
  100.      * @ignore
  101.      */         
  102.     public function __construct()
  103.     {
  104.     }
  105.  
  106.     /**
  107.      * Count number of hits for the most frequented letters in Arabic language
  108.      * (Alef, Lam and Yaa), then calculate association ratio with each of
  109.      * possible character set (UTF-8, Windows-1256 and ISO-8859-6)
  110.      *                           
  111.      * @param String $string Arabic string in unknown format
  112.      *      
  113.      * @return Array Character set as key and string association ratio as value
  114.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  115.      */
  116.     public function guess($string)
  117.     {
  118.         // The most frequent Arabic letters are Alef, Lam, and Yeh
  119.         $charset['windows-1256']  substr_count($stringchr(199));
  120.         $charset['windows-1256'+= substr_count($stringchr(225));
  121.         $charset['windows-1256'+= substr_count($stringchr(237));
  122.  
  123.         $charset['iso-8859-6']  substr_count($stringchr(199));
  124.         $charset['iso-8859-6'+= substr_count($stringchr(228));
  125.         $charset['iso-8859-6'+= substr_count($stringchr(234));
  126.         
  127.         $charset['utf-8']  substr_count($stringchr(216).chr(167));
  128.         $charset['utf-8'+= substr_count($stringchr(217).chr(132));
  129.         $charset['utf-8'+= substr_count($stringchr(217).chr(138));
  130.         
  131.         $total $charset['windows-1256'
  132.                  $charset['iso-8859-6'
  133.                  $charset['utf-8'];
  134.         
  135.         $charset['windows-1256'round($charset['windows-1256'100 $total);
  136.         $charset['iso-8859-6']   round($charset['iso-8859-6'100 $total);
  137.         $charset['utf-8']        round($charset['utf-8'100 $total);
  138.         
  139.         return $charset;
  140.     }
  141.     
  142.     /**
  143.      * Find the most possible character set for given Arabic string in unknown
  144.      * format
  145.      *         
  146.      * @param String $string Arabic string in unknown format
  147.      *      
  148.      * @return String The most possible character set for given Arabic string in
  149.      *                 unknown format[utf-8|windows-1256|iso-8859-6]
  150.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  151.      */
  152.     public function getCharset($string)
  153.     {
  154.         if (preg_match('/<meta .* charset=([^\"]+)".*>/sim'$string$matches)) {
  155.             $value $matches[1];
  156.         else {
  157.             $charset $this->guess($string);
  158.             arsort($charset);
  159.             $value key($charset);
  160.         }
  161.  
  162.         return $value;
  163.     }
  164. }

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