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

Source for file KeySwap.php

Documentation is available at KeySwap.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 Keyboard Swapping Language
  31.  *  
  32.  * Filename:   KeySwap.php
  33.  *  
  34.  * Original    Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:    Convert keyboard language programmatically (English - Arabic)
  37.  *  
  38.  * ----------------------------------------------------------------------
  39.  *  
  40.  * Arabic Keyboard Swapping Language
  41.  *
  42.  * PHP class to convert keyboard language between English and Arabic
  43.  * programmatically. This function can be helpful in dual language forms when
  44.  * users miss change keyboard language while they are entering data.
  45.  * 
  46.  * If you wrote an Arabic sentence while your keyboard stays in English mode by
  47.  * mistake, you will get a non-sense English text on your PC screen. In that case
  48.  * you can use this class to make a kind of magic conversion to swap that odd text
  49.  * by original Arabic sentence you meant when you type on your keyboard.
  50.  * 
  51.  * Please note that magic conversion in the opposite direction (if you type English
  52.  * sentences while your keyboard stays in Arabic mode) is also available in this
  53.  * class, but it is not reliable as much as previous case because in Arabic keyboard
  54.  * we have some keys provide a shortcut to type two chars in one click (those keys
  55.  * include: b, B, G and T).
  56.  * 
  57.  * Well, we try in this class to come over this issue by suppose that user used
  58.  * optimum way by using shortcut keys when available instead of assemble chars using
  59.  * stand alone keys, but if (s)he does not then you may have some typo chars in
  60.  * converted text.
  61.  * 
  62.  * Example:
  63.  * <code>
  64.  *     include('./I18N/Arabic.php');
  65.  *     $obj = new I18N_Arabic('KeySwap');
  66.  * 
  67.  *     $str = "Hpf lk hgkhs hglj'vtdkK Hpf hg`dk dldg,k f;gdjil Ygn ,p]hkdm ...";
  68.  * 
  69.  *     echo "<p><u><i>Before:</i></u><br />$str<br /><br />";
  70.  *     
  71.  *     $text = $obj->swap_ea($str);
  72.  *        
  73.  *     echo "<u><i>After:</i></u><br />$text<br /><br />";
  74.  * </code>
  75.  *
  76.  * @category  I18N
  77.  * @package   I18N_Arabic
  78.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  79.  * @copyright 2006-2013 Khaled Al-Sham'aa
  80.  *    
  81.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  82.  * @link      http://www.ar-php.org
  83.  */
  84.  
  85. // New in PHP V5.3: Namespaces
  86. // namespace I18N\Arabic;
  87. // 
  88. // $obj = new I18N\Arabic\KeySwap();
  89. // 
  90. // use I18N\Arabic;
  91. // $obj = new Arabic\KeySwap();
  92. //
  93. // use I18N\Arabic\KeySwap as KeySwap;
  94. // $obj = new KeySwap();
  95.  
  96. /**
  97.  * This PHP class convert keyboard language programmatically (English - Arabic)
  98.  *  
  99.  * @category  I18N
  100.  * @package   I18N_Arabic
  101.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  102.  * @copyright 2006-2013 Khaled Al-Sham'aa
  103.  *    
  104.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  105.  * @link      http://www.ar-php.org
  106.  */ 
  107. {
  108.     // First 12 chars replaced by 1 Byte in Arabic keyboard 
  109.     // while rest replaced by 2 Bytes UTF8
  110.     private static $_swapEn '{}DFL:"ZCV<>`qwertyuiop[]asdfghjkl;\'zxcvnm,./~QWERYIOPASHJKXN?M';
  111.     private static $_swapAr '<>][/:"~}{,.ذضصثقفغعهخحجدشسيبلاتنمكطئءؤرىةوزظًٌَُّإ÷×؛ٍِأـ،ْآ؟’';
  112.     
  113.     private static $_swapFr       '²azertyuiop^$qsdfghjklmù*<wxcvn,;:!²1234567890°+AZERYIOP¨£QSDFHJKLM%µ<WXCVN?./§';
  114.     private static $_swapArAzerty '>ضصثقفغعهخحجدشسيبلاتنمكطذ\\ئءؤرىةوزظ>&é"\'(-è_çà)=ضصثقغهخحجدشسيباتنمكطذ\\ئءؤرىةوزظ';  
  115.  
  116.     private $_transliteration array();
  117.     
  118.     /**
  119.      * Loads initialize values
  120.      *
  121.      * @ignore
  122.      */         
  123.     public function __construct()
  124.     {
  125.         $xml simplexml_load_file(dirname(__FILE__).'/data/charset/arabizi.xml');
  126.         
  127.         foreach ($xml->transliteration->item as $item{
  128.             $index $item['id'];
  129.             $this->_transliteration["$index"= (string)$item;
  130.         
  131.     }
  132.     
  133.     /**
  134.      * Make conversion to swap that odd Arabic text by original English sentence
  135.      * you meant when you type on your keyboard (if keyboard language was
  136.      * incorrect)
  137.      *          
  138.      * @param string $text Odd Arabic string
  139.      *                    
  140.      * @return string Normal English string
  141.      * @author Khaled Al-Sham'aa
  142.      */
  143.     public static function swapAe($text)
  144.     {
  145.         $output '';
  146.         
  147.         $text stripslashes($text);
  148.  
  149.         $text str_replace('لا''b'$text);
  150.         $text str_replace('لآ''B'$text);
  151.         $text str_replace('لأ''G'$text);
  152.         $text str_replace('لإ''T'$text);
  153.         $text str_replace('‘''U'$text);
  154.         
  155.         $max strlen($text);
  156.         
  157.         for ($i=0$i<$max$i++{
  158.  
  159.             $pos strpos(self::$_swapAr$text[$i]);
  160.  
  161.             if ($pos === false{
  162.                 $output .= $text[$i];
  163.             else {
  164.                 $pos2 strpos(self::$_swapAr$text[$i].$text[$i+1]);
  165.                 if ($pos2 !== false{
  166.                     $pos $pos2;
  167.                     $i++;
  168.                 }
  169.  
  170.                 if ($pos 12{
  171.                     $adjPos $pos;
  172.                 else {
  173.                     $adjPos ($pos 12)/12;
  174.                 }
  175.  
  176.                 $output .= substr(self::$_swapEn$adjPos1);
  177.             }
  178.  
  179.         }
  180.         
  181.         return $output;
  182.     }
  183.     
  184.     /**
  185.      * Make conversion to swap that odd English text by original Arabic sentence
  186.      * you meant when you type on your keyboard (if keyboard language was
  187.      * incorrect)
  188.      *           
  189.      * @param string $text Odd English string
  190.      *                    
  191.      * @return string Normal Arabic string
  192.      * @author Khaled Al-Sham'aa
  193.      */
  194.     public static function swapEa($text)
  195.     {
  196.         $output '';
  197.         
  198.         $text stripslashes($text);
  199.         
  200.         $text str_replace('b''لا'$text);
  201.         $text str_replace('B''لآ'$text);
  202.         $text str_replace('G''لأ'$text);
  203.         $text str_replace('T''لإ'$text);
  204.         $text str_replace('U''‘'$text);
  205.         
  206.         $max strlen($text);
  207.         
  208.         for ($i=0$i<$max$i++{
  209.             $pos strpos(self::$_swapEn$text[$i]);
  210.             if ($pos === false{
  211.                 $output .= $text[$i];
  212.             else {
  213.                 if ($pos 12{
  214.                     $adjPos $pos;
  215.                     $len    1;
  216.                 else {
  217.                     $adjPos ($pos 12)*12;
  218.                     $len    2
  219.                 }
  220.                 if ($adjPos == 112{
  221.                     $len 3;
  222.                 }
  223.                 $output .= substr(self::$_swapAr$adjPos$len);
  224.             }
  225.         }
  226.         
  227.         return $output;
  228.     }
  229. }

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