. * * ---------------------------------------------------------------------- * * Class Name: Arabic Text ArStandard Class * * Filename: Standard.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('./I18N/Arabic.php'); * $obj = new I18N_Arabic('Standard'); * * $str = $obj->standard($content); * * echo $str; * * * @category I18N * @package I18N_Arabic * @author Khaled Al-Sham'aa * @copyright 2006-2016 Khaled Al-Sham'aa * * @license LGPL * @link http://www.ar-php.org */ /** * This PHP class standardize Arabic text * * @category I18N * @package I18N_Arabic * @author Khaled Al-Sham'aa * @copyright 2006-2016 Khaled Al-Sham'aa * * @license LGPL * @link http://www.ar-php.org */ class I18N_Arabic_Standard { /** * Loads initialize values * * @ignore */ 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-Sham'aa */ public static function standard($text) { $patterns = array(); $replacements = array(); array_push($patterns, '/\r\n/u', '/([^\@])\n([^\@])/u', '/\r/u'); array_push($replacements, "\n@@@\n", "\\1\n&&&\n\\2", "\n###\n"); /** * النقطة، الفاصلة، الفاصلة المنقوطة، * النقطتان، علامتي الاستفهام والتعجب، * النقاط الثلاث المتتالية * يترك فراغ واحد بعدها جميعا * دون أي فراغ قبلها */ 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 '); array_push($patterns, '/\n?@@@\n?/u', '/\n?&&&\n?/u', '/\n?###\n?/u'); array_push($replacements, "\r\n", "\n", "\r"); $text = preg_replace($patterns, $replacements, $text); return $text; } }