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

Source for file Standard.php

Documentation is available at Standard.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 Text ArStandard Class
  31.  *  
  32.  * Filename: Standard.php
  33.  *  
  34.  * Original  Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:  Standardize Arabic text
  37.  *  
  38.  * ----------------------------------------------------------------------
  39.  *  
  40.  * Arabic Text Standardize Class
  41.  *
  42.  * PHP class to standardize Arabic text just like rules followed in magazines
  43.  * and newspapers like spaces before and after punctuations, brackets and
  44.  * units etc ...
  45.  *
  46.  * Example:
  47.  * <code>
  48.  *     include('./I18N/Arabic.php');
  49.  *     $obj = new I18N_Arabic('Standard');
  50.  * 
  51.  *     $str = $obj->standard($content);
  52.  *       
  53.  *     echo $str;
  54.  * </code>
  55.  *
  56.  * @category  I18N
  57.  * @package   I18N_Arabic
  58.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  59.  * @copyright 2006-2012 Khaled Al-Sham'aa
  60.  *    
  61.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  62.  * @link      http://www.ar-php.org
  63.  */
  64.  
  65. // New in PHP V5.3: Namespaces
  66. // namespace I18N\Arabic;
  67. // 
  68. // $obj = new I18N\Arabic\Standard();
  69. // 
  70. // use I18N\Arabic;
  71. // $obj = new Arabic\Standard();
  72. //
  73. // use I18N\Arabic\Standard as Standard;
  74. // $obj = new Standard();
  75.  
  76. /**
  77.  * This PHP class standardize Arabic text
  78.  *  
  79.  * @category  I18N
  80.  * @package   I18N_Arabic
  81.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  82.  * @copyright 2006-2012 Khaled Al-Sham'aa
  83.  *    
  84.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  85.  * @link      http://www.ar-php.org
  86.  */ 
  87. {
  88.     /**
  89.      * Loads initialize values
  90.      *
  91.      * @ignore
  92.      */         
  93.     public function __construct()
  94.     {
  95.     }
  96.  
  97.     /**
  98.      * This method will standardize Arabic text to follow writing standards
  99.      * (just like magazine rules)
  100.      *          
  101.      * @param string $text Arabic text you would like to standardize
  102.      *                    
  103.      * @return String Standardized version of input Arabic text
  104.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  105.      */
  106.     public static function standard($text)
  107.     {
  108.         $patterns     array();
  109.         $replacements array();
  110.         
  111.         /**
  112.          * النقطة، الفاصلة، الفاصلة المنقوطة،
  113.          * النقطتان، علامتي الاستفهام والتعجب،
  114.          * النقاط الثلاث المتتالية
  115.          * يترك فراغ واحد بعدها جميعا
  116.          * دون أي فراغ قبلها
  117.          */
  118.         array_push($patterns'/\s*([\.\،\؛\:\!\؟])\s*/u');
  119.         array_push($replacements'\\1 ');
  120.  
  121.         /**
  122.          * النقاط المتتالية عددها 3 فقط
  123.          * (ليست نقطتان وليست أربع أو أكثر)
  124.          */
  125.         array_push($patterns'/(\. ){2,}/u');
  126.         array_push($replacements'...');
  127.  
  128.         /**
  129.          * الأقواس ( ) [ ] { } يترك قبلها وبعدها فراغ
  130.          * وحيد، فيما لا يوجد بينها وبين ما بداخلها
  131.          * أي فراغ
  132.          */
  133.         array_push($patterns'/\s*([\(\{\[])\s*/u');
  134.         array_push($replacements' \\1');
  135.  
  136.         array_push($patterns'/\s*([\)\}\]])\s*/u');
  137.         array_push($replacements'\\1 ');
  138.  
  139.         /**
  140.          * علامات الاقتباس "..."
  141.          * يترك قبلها وبعدها فراغ
  142.          * وحيد، فيما لا يوجد بينها
  143.          * وبين ما بداخلها أي فراغ
  144.          */
  145.         array_push($patterns'/\s*\"\s*(.+)((?<!\s)\"|\s+\")\s*/u');
  146.         array_push($replacements' "\\1" ');
  147.  
  148.         /**
  149.          * علامات الإعتراض -...-
  150.          * يترك قبلها وبعدها فراغ
  151.          * وحيد، فيما لا يوجد بينها
  152.          * وبين ما بداخلها أي فراغ
  153.          */
  154.         array_push($patterns'/\s*\-\s*(.+)((?<!\s)\-|\s+\-)\s*/u');
  155.         array_push($replacements' -\\1- ');
  156.  
  157.         /**
  158.          * لا يترك فراغ بين حرف العطف الواو وبين
  159.          * الكلمة التي تليه
  160.          * إلا إن كانت تبدأ بحرف الواو
  161.          */
  162.         array_push($patterns'/\sو\s+([^و])/u');
  163.         array_push($replacements' و\\1');
  164.  
  165.         /**
  166.          * الواحدات الإنجليزية توضع
  167.          * على يمين الرقم مع ترك فراغ
  168.          */
  169.         array_push($patterns'/\s+(\w+)\s*(\d+)\s+/');
  170.         array_push($replacements' <span dir="ltr">\\2 \\1</span> ');
  171.  
  172.         array_push($patterns'/\s+(\d+)\s*(\w+)\s+/');
  173.         array_push($replacements' <span dir="ltr">\\1 \\2</span> ');
  174.  
  175.         /**
  176.          * النسبة المؤية دائما إلى يسار الرقم
  177.          * وبدون أي فراغ يفصل بينهما 40% مثلا
  178.          */
  179.         array_push($patterns'/\s+(\d+)\s*\%\s+/u');
  180.         array_push($replacements' %\\1 ');
  181.  
  182.         $text preg_replace($patterns$replacements$text);
  183.  
  184.         return $text;
  185.     }
  186. }

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