. * * ---------------------------------------------------------------------- * * Class Name: Arabic Text ArStandard Class * * Filename: ArStandard.class.php * * Original Author(s): Khaled Al-Sham'aa * * Purpose: Standardize Arabic text * * ---------------------------------------------------------------------- * * Arabic Text Standardize Class * * PHP class to standardize Arabic text just like rules followed in magazines * and newspapers like spaces before and after punctuations, brackets and * units etc ... * * Example: * * include('./Arabic.php'); * $obj = new Arabic('ArStandard'); * * $str = $obj->standard($content); * * echo $str; * * * @category I18N * @package Arabic * @author Khaled Al-Shamaa * @copyright 2006-2010 Khaled Al-Shamaa * * @license LGPL * @link http://www.ar-php.org */ // New in PHP V5.3: Namespaces // namespace I18N/Arabic/ArStandard; /** * This PHP class standardize Arabic text * * @category I18N * @package Arabic * @author Khaled Al-Shamaa * @copyright 2006-2010 Khaled Al-Shamaa * * @license LGPL * @link http://www.ar-php.org */ class ArStandard { /** * "standard" method output charset * @var String */ public $standardOutput = 'utf-8'; /** * "standard" method input charset * @var String */ public $standardInput = 'utf-8'; /** * Name of the textual "standard" method parameters * @var Array */ public $standardVars = array('text'); /** * Loads initialize values */ public function __construct() { } /** * This method will standardize Arabic text to follow writing standards * (just like magazine rules) * * @param string $text Arabic text you would like to standardize * * @return String Standardized version of input Arabic text * @author Khaled Al-Shamaa */ public static function standard($text) { $patterns = array(); $replacements = array(); /** * النقطة، الفاصلة، الفاصلة المنقوطة، * النقطتان، علامتي الاستفهام والتعجب، * النقاط الثلاث المتتالية * يترك فراغ واحد بعدها جميعا * دون أي فراغ قبلها */ array_push($patterns, '/\s*([\.\،\؛\:\!\؟])\s*/u'); array_push($replacements, '\\1 '); /** * النقاط المتتالية عددها 3 فقط * (ليست نقطتان وليست أربع أو أكثر) */ array_push($patterns, '/(\. ){2,}/u'); array_push($replacements, '...'); /** * الأقواس ( ) [ ] { } يترك قبلها وبعدها فراغ * وحيد، فيما لا يوجد بينها وبين ما بداخلها * أي فراغ */ array_push($patterns, '/\s*([\(\{\[])\s*/u'); array_push($replacements, ' \\1'); array_push($patterns, '/\s*([\)\}\]])\s*/u'); array_push($replacements, '\\1 '); /** * علامات الاقتباس "..." * يترك قبلها وبعدها فراغ * وحيد، فيما لا يوجد بينها * وبين ما بداخلها أي فراغ */ array_push($patterns, '/\s*\"\s*(.+)((?\\2 \\1 '); array_push($patterns, '/\s+(\d+)\s*(\w+)\s+/'); array_push($replacements, ' \\1 \\2 '); /** * النسبة المؤية دائما إلى يسار الرقم * وبدون أي فراغ يفصل بينهما 40% مثلا */ array_push($patterns, '/\s+(\d+)\s*\%\s+/u'); array_push($replacements, ' %\\1 '); $text = preg_replace($patterns, $replacements, $text); return $text; } }