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-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 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-2013 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-2013 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.         array_push($patterns'/\r\n/u''/([^\@])\n([^\@])/u''/\r/u');
  112.         array_push($replacements"\n@@@\n""\\1\n&&&\n\\2""\n###\n");
  113.         
  114.         /**
  115.          * النقطة، الفاصلة، الفاصلة المنقوطة،
  116.          * النقطتان، علامتي الاستفهام والتعجب،
  117.          * النقاط الثلاث المتتالية
  118.          * يترك فراغ واحد بعدها جميعا
  119.          * دون أي فراغ قبلها
  120.          */
  121.         array_push($patterns'/\s*([\.\،\؛\:\!\؟])\s*/u');
  122.         array_push($replacements'\\1 ');
  123.  
  124.         /**
  125.          * النقاط المتتالية عددها 3 فقط
  126.          * (ليست نقطتان وليست أربع أو أكثر)
  127.          */
  128.         array_push($patterns'/(\. ){2,}/u');
  129.         array_push($replacements'...');
  130.  
  131.         /**
  132.          * الأقواس ( ) [ ] { } يترك قبلها وبعدها فراغ
  133.          * وحيد، فيما لا يوجد بينها وبين ما بداخلها
  134.          * أي فراغ
  135.          */
  136.         array_push($patterns'/\s*([\(\{\[])\s*/u');
  137.         array_push($replacements' \\1');
  138.  
  139.         array_push($patterns'/\s*([\)\}\]])\s*/u');
  140.         array_push($replacements'\\1 ');
  141.  
  142.         /**
  143.          * علامات الاقتباس "..."
  144.          * يترك قبلها وبعدها فراغ
  145.          * وحيد، فيما لا يوجد بينها
  146.          * وبين ما بداخلها أي فراغ
  147.          */
  148.         array_push($patterns'/\s*\"\s*(.+)((?<!\s)\"|\s+\")\s*/u');
  149.         array_push($replacements' "\\1" ');
  150.  
  151.         /**
  152.          * علامات الإعتراض -...-
  153.          * يترك قبلها وبعدها فراغ
  154.          * وحيد، فيما لا يوجد بينها
  155.          * وبين ما بداخلها أي فراغ
  156.          */
  157.         array_push($patterns'/\s*\-\s*(.+)((?<!\s)\-|\s+\-)\s*/u');
  158.         array_push($replacements' -\\1- ');
  159.  
  160.         /**
  161.          * لا يترك فراغ بين حرف العطف الواو وبين
  162.          * الكلمة التي تليه
  163.          * إلا إن كانت تبدأ بحرف الواو
  164.          */
  165.         array_push($patterns'/\sو\s+([^و])/u');
  166.         array_push($replacements' و\\1');
  167.  
  168.         /**
  169.          * الواحدات الإنجليزية توضع
  170.          * على يمين الرقم مع ترك فراغ
  171.          */
  172.         array_push($patterns'/\s+(\w+)\s*(\d+)\s+/');
  173.         array_push($replacements' <span dir="ltr">\\2 \\1</span> ');
  174.  
  175.         array_push($patterns'/\s+(\d+)\s*(\w+)\s+/');
  176.         array_push($replacements' <span dir="ltr">\\1 \\2</span> ');
  177.  
  178.         /**
  179.          * النسبة المؤية دائما إلى يسار الرقم
  180.          * وبدون أي فراغ يفصل بينهما 40% مثلا
  181.          */
  182.         array_push($patterns'/\s+(\d+)\s*\%\s+/u');
  183.         array_push($replacements' %\\1 ');
  184.         
  185.         array_push($patterns'/\n?@@@\n?/u''/\n?&&&\n?/u''/\n?###\n?/u');
  186.         array_push($replacements"\r\n""\n""\r");
  187.  
  188.         $text preg_replace($patterns$replacements$text);
  189.  
  190.         return $text;
  191.     }
  192. }

Documentation generated on Mon, 14 Jan 2013 17:49:07 +0100 by phpDocumentor 1.4.0