Changeset 205328
- Timestamp:
- 02/14/2010 03:35:01 PM (16 years ago)
- Location:
- hijri/trunk
- Files:
-
- 4 edited
-
ArDate.class.php (modified) (1 diff)
-
Arabic.php (modified) (1 diff)
-
hijri.php (modified) (2 diffs)
-
readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
hijri/trunk/ArDate.class.php
r101629 r205328 1 1 <?php 2 // ---------------------------------------------------------------------- 3 // Copyright (C) 2008 by Khaled Al-Shamaa. 4 // http://www.ar-php.org/ 5 // ---------------------------------------------------------------------- 6 // LICENSE 7 8 // This program is open source product; you can redistribute it and/or 9 // modify it under the terms of the GNU Lesser General Public License (LGPL) 10 // as published by the Free Software Foundation; either version 3 11 // of the License, or (at your option) any later version. 12 13 // This program is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 // GNU Lesser General Public License for more details. 17 18 // You should have received a copy of the GNU Lesser General Public License 19 // along with this program. If not, see <http://www.gnu.org/licenses/>. 20 // ---------------------------------------------------------------------- 21 // Class Name: Arabic Date 22 // Filename: ArDate.class.php 23 // Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org> 24 // Purpose: Arabic customization for PHP date function 25 // ---------------------------------------------------------------------- 26 27 /** 28 * Arabic Date 29 * 30 * PHP class for Arabic and Islamic customization of PHP date function. It 31 * can convert UNIX timestamp into string in Arabic as well as convert it into 32 * Hijri calendar 33 * 34 * The Islamic Calendar: 35 * 36 * The Islamic calendar is purely lunar and consists of twelve alternating months 37 * of 30 and 29 days, with the final 29 day month extended to 30 days during leap 38 * years. Leap years follow a 30 year cycle and occur in years 1, 5, 7, 10, 13, 16, 39 * 18, 21, 24, 26, and 29. The calendar begins on Friday, July 16th, 622 C.E. in 40 * the Julian calendar, Julian day 1948439.5, the day of Muhammad's separate from 41 * Mecca to Medina, the first day of the first month of year 1 A.H.--"Anno Hegira". 42 * 43 * Each cycle of 30 years thus contains 19 normal years of 354 days and 11 leap 44 * years of 355, so the average length of a year is therefore 45 * ((19 x 354) + (11 x 355)) / 30 = 354.365... days, with a mean length of month of 46 * 1/12 this figure, or 29.53055... days, which closely approximates the mean 47 * synodic month (time from new Moon to next new Moon) of 29.530588 days, with the 48 * calendar only slipping one day with respect to the Moon every 2525 years. Since 49 * the calendar is fixed to the Moon, not the solar year, the months shift with 50 * respect to the seasons, with each month beginning about 11 days earlier in each 51 * successive solar year. 52 * 53 * The convert presented here is the most commonly used civil calendar in the Islamic 54 * world; for religious purposes months are defined to start with the first 55 * observation of the crescent of the new Moon. 56 * 57 * The Julian Calendar: 58 * 59 * The Julian calendar was proclaimed by Julius Casar in 46 B.C. and underwent 60 * several modifications before reaching its final form in 8 C.E. The Julian 61 * calendar differs from the Gregorian only in the determination of leap years, 62 * lacking the correction for years divisible by 100 and 400 in the Gregorian 63 * calendar. In the Julian calendar, any positive year is a leap year if divisible 64 * by 4. (Negative years are leap years if when divided by 4 a remainder of 3 65 * results.) Days are considered to begin at midnight. 66 * 67 * In the Julian calendar the average year has a length of 365.25 days. compared to 68 * the actual solar tropical year of 365.24219878 days. The calendar thus 69 * accumulates one day of error with respect to the solar year every 128 years. 70 * Being a purely solar calendar, no attempt is made to synchronise the start of 71 * months to the phases of the Moon. 72 * 73 * The Gregorian Calendar: 74 * 75 * The Gregorian calendar was proclaimed by Pope Gregory XIII and took effect in 76 * most Catholic states in 1582, in which October 4, 1582 of the Julian calendar 77 * was followed by October 15 in the new calendar, correcting for the accumulated 78 * discrepancy between the Julian calendar and the equinox as of that date. When 79 * comparing historical dates, it's important to note that the Gregorian calendar, 80 * used universally today in Western countries and in international commerce, was 81 * adopted at different times by different countries. Britain and her colonies 82 * (including what is now the United States), did not switch to the Gregorian 83 * calendar until 1752, when Wednesday 2nd September in the Julian calendar dawned 84 * as Thursday the 14th in the Gregorian. 85 * 86 * The Gregorian calendar is a minor correction to the Julian. In the Julian 87 * calendar every fourth year is a leap year in which February has 29, not 28 days, 88 * but in the Gregorian, years divisible by 100 are not leap years unless they are 89 * also divisible by 400. How prescient was Pope Gregory! Whatever the problems of 90 * Y2K, they won't include sloppy programming which assumes every year divisible by 91 * 4 is a leap year since 2000, unlike the previous and subsequent years divisible 92 * by 100, is a leap year. As in the Julian calendar, days are considered to begin 93 * at midnight. 94 * 95 * The average length of a year in the Gregorian calendar is 365.2425 days compared 96 * to the actual solar tropical year (time from equinox to equinox) of 365.24219878 97 * days, so the calendar accumulates one day of error with respect to the solar year 98 * about every 3300 years. As a purely solar calendar, no attempt is made to 99 * synchronise the start of months to the phases of the Moon. 100 * 101 * date -- Format a local time/date 102 * string date ( string format, int timestamp); 103 * 104 * Returns a string formatted according to the given format string using the given 105 * integer timestamp or the current local time if no timestamp is given. In 106 * otherwords, timestamp is optional and defaults to the value of time(). 107 * 108 * Example: 109 * <code> 110 * date_default_timezone_set('UTC'); 111 * $time = time(); 112 * 113 * echo date('l dS F Y h:i:s A', $time); 114 * echo '<br /><br />'; 115 * 116 * include('Arabic.php'); 117 * $Ar = new Arabic('ArDate'); 118 * 119 * echo $Ar->date('l dS F Y h:i:s A', $time); 120 * echo '<br /><br />'; 121 * 122 * $Ar->ArDate->setMode(2); 123 * echo $Ar->date('l dS F Y h:i:s A', $time); 124 * echo '<br /><br />'; 125 * 126 * $Ar->ArDate->setMode(3); 127 * echo $Ar->date('l dS F Y h:i:s A', $time); 128 * echo '<br /><br />'; 129 * 130 * $Ar->ArDate->setMode(4); 131 * echo $Ar->date('l dS F Y h:i:s A', $time); 132 * </code> 133 * 134 * @copyright Khaled Al-Shamaa 2008 135 * @link http://www.ar-php.org 136 * @author Khaled Al-Shamaa <khaled@ar-php.org> 137 * @package Arabic 138 */ 139 140 141 class ArDate 142 { 143 private $mode = 1; 144 private static $ISLAMIC_EPOCH = 1948439.5; 145 146 /** 147 * @return TRUE if success, or FALSE if fail 148 * @param Integer $mode Output mode of date function where: 149 * 1) Hegri format (Islamic calendar) 150 * 2) Arabic month names used in Middle East countries 151 * 3) Arabic Transliteration of Gregorian month names 152 * 4) Both of 2 and 3 formats together 153 * 5) Libyan way 154 * @desc Setting value for $mode scalar 155 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 156 */ 157 public function setMode($mode = 1) 158 { 159 $flag = true; 160 161 $mode = (int) $mode; 162 163 if ($mode > 0 && $mode < 6) { 164 $this->mode = $mode; 165 } else { 166 167 $flag = false; 168 } 169 170 return $flag; 171 } 172 173 /** 174 * @return Integer Value of $mode properity 175 * @desc Getting $mode value that refer to output mode format 176 * 1) Hegri format (Islamic calendar) 177 * 2) Arabic month names used in Middle East countries 178 * 3) Arabic Transliteration of Gregorian month names 179 * 4) Both of 2 and 3 formats together 180 * 5) Libyan way 181 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 182 */ 183 public function getMode() 184 { 185 return $this->mode; 186 } 187 188 /** 189 * @return String Format Arabic date string according to given format string 190 * using the given integer timestamp or the current local 191 * time if no timestamp is given. 192 * @param String $format Format string (same as PHP date function) 193 * @param Integer $timestamp Unix timestamp (default is current local time) 194 * @desc Format a local time/date in Arabic string 195 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 196 */ 197 public function date($format, $timestamp = time) 198 { 199 if ($this->mode == 1) { 200 $hj_txt_month[1] = 'محرم'; 201 $hj_txt_month[2] = 'صفر'; 202 $hj_txt_month[3] = 'ربيع الأول'; 203 $hj_txt_month[4] = 'ربيع الثاني'; 204 $hj_txt_month[5] = 'جمادى الأولى'; 205 $hj_txt_month[6] = 'جمادى الثانية'; 206 $hj_txt_month[7] = 'رجب'; 207 $hj_txt_month[8] = 'شعبان'; 208 $hj_txt_month[9] = 'رمضان'; 209 $hj_txt_month[10] = 'شوال'; 210 $hj_txt_month[11] = 'ذو القعدة'; 211 $hj_txt_month[12] = 'ذو الحجة'; 212 213 $patterns = array(); 214 $replacements = array(); 215 216 array_push($patterns, 'Y'); 217 array_push($replacements, 'x1'); 218 array_push($patterns, 'y'); 219 array_push($replacements, 'x2'); 220 array_push($patterns, 'M'); 221 array_push($replacements, 'x3'); 222 array_push($patterns, 'F'); 223 array_push($replacements, 'x3'); 224 array_push($patterns, 'n'); 225 array_push($replacements, 'x4'); 226 array_push($patterns, 'm'); 227 array_push($replacements, 'x5'); 228 array_push($patterns, 'j'); 229 array_push($replacements, 'x6'); 230 array_push($patterns, 'd'); 231 array_push($replacements, 'x7'); 232 233 $format = str_replace($patterns, $replacements, $format); 234 235 $str = date($format, $timestamp); 236 $str = $this->en2ar($str); 237 238 list($Y, $M, $D) = split(' ', date('Y m d', $timestamp)); 239 240 list($hj_y, $hj_m, $hj_d) = $this->hj_convert($Y, $M, $D); 241 242 $patterns = array(); 243 $replacements = array(); 244 245 array_push($patterns, 'x1'); 246 array_push($replacements, $hj_y); 247 array_push($patterns, 'x2'); 248 array_push($replacements, substr($hj_y, -2)); 249 array_push($patterns, 'x3'); 250 array_push($replacements, $hj_txt_month[$hj_m]); 251 array_push($patterns, 'x4'); 252 array_push($replacements, $hj_m); 253 array_push($patterns, 'x5'); 254 array_push($replacements, sprintf('%02d', $hj_m)); 255 array_push($patterns, 'x6'); 256 array_push($replacements, $hj_d); 257 array_push($patterns, 'x7'); 258 array_push($replacements, sprintf('%02d', $hj_d)); 259 260 $str = str_replace($patterns, $replacements, $str); 261 } elseif($this->mode == 5) { 262 $year = date('Y', $timestamp); 263 $year -= 632; 264 $yr = substr("$year", -2); 265 266 $format = str_replace('Y', $year, $format); 267 $format = str_replace('y', $yr, $format); 268 269 $str = date($format, $timestamp); 270 $str = $this->en2ar($str); 2 /** 3 * ---------------------------------------------------------------------- 4 * 5 * Copyright (C) 2009 by Khaled Al-Shamaa. 6 * 7 * http://www.ar-php.org 8 * 9 * ---------------------------------------------------------------------- 10 * 11 * LICENSE 12 * 13 * This program is open source product; you can redistribute it and/or 14 * modify it under the terms of the GNU Lesser General Public License (LGPL) 15 * as published by the Free Software Foundation; either version 3 16 * of the License, or (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU Lesser General Public License for more details. 22 * 23 * You should have received a copy of the GNU Lesser General Public License 24 * along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>. 25 * 26 * ---------------------------------------------------------------------- 27 * 28 * Class Name: Arabic Date 29 * 30 * Filename: ArDate.class.php 31 * 32 * Original Author(s): Khaled Al-Sham'aa <khaled.alshamaa@gmail.com> 33 * 34 * Purpose: Arabic customization for PHP date function 35 * 36 * ---------------------------------------------------------------------- 37 * 38 * Arabic Date 39 * 40 * PHP class for Arabic and Islamic customization of PHP date function. It 41 * can convert UNIX timestamp into string in Arabic as well as convert it into 42 * Hijri calendar 43 * 44 * The Islamic Calendar: 45 * 46 * The Islamic calendar is purely lunar and consists of twelve alternating months 47 * of 30 and 29 days, with the final 29 day month extended to 30 days during leap 48 * years. Leap years follow a 30 year cycle and occur in years 1, 5, 7, 10, 13, 16, 49 * 18, 21, 24, 26, and 29. The calendar begins on Friday, July 16th, 622 C.E. in 50 * the Julian calendar, Julian day 1948439.5, the day of Muhammad's separate from 51 * Mecca to Medina, the first day of the first month of year 1 A.H.--"Anno Hegira". 52 * 53 * Each cycle of 30 years thus contains 19 normal years of 354 days and 11 leap 54 * years of 355, so the average length of a year is therefore 55 * ((19 x 354) + (11 x 355)) / 30 = 354.365... days, with a mean length of month of 56 * 1/12 this figure, or 29.53055... days, which closely approximates the mean 57 * synodic month (time from new Moon to next new Moon) of 29.530588 days, with the 58 * calendar only slipping one day with respect to the Moon every 2525 years. Since 59 * the calendar is fixed to the Moon, not the solar year, the months shift with 60 * respect to the seasons, with each month beginning about 11 days earlier in each 61 * successive solar year. 62 * 63 * The convert presented here is the most commonly used civil calendar in the Islamic 64 * world; for religious purposes months are defined to start with the first 65 * observation of the crescent of the new Moon. 66 * 67 * The Julian Calendar: 68 * 69 * The Julian calendar was proclaimed by Julius Casar in 46 B.C. and underwent 70 * several modifications before reaching its final form in 8 C.E. The Julian 71 * calendar differs from the Gregorian only in the determination of leap years, 72 * lacking the correction for years divisible by 100 and 400 in the Gregorian 73 * calendar. In the Julian calendar, any positive year is a leap year if divisible 74 * by 4. (Negative years are leap years if when divided by 4 a remainder of 3 75 * results.) Days are considered to begin at midnight. 76 * 77 * In the Julian calendar the average year has a length of 365.25 days. compared to 78 * the actual solar tropical year of 365.24219878 days. The calendar thus 79 * accumulates one day of error with respect to the solar year every 128 years. 80 * Being a purely solar calendar, no attempt is made to synchronise the start of 81 * months to the phases of the Moon. 82 * 83 * The Gregorian Calendar: 84 * 85 * The Gregorian calendar was proclaimed by Pope Gregory XIII and took effect in 86 * most Catholic states in 1582, in which October 4, 1582 of the Julian calendar 87 * was followed by October 15 in the new calendar, correcting for the accumulated 88 * discrepancy between the Julian calendar and the equinox as of that date. When 89 * comparing historical dates, it's important to note that the Gregorian calendar, 90 * used universally today in Western countries and in international commerce, was 91 * adopted at different times by different countries. Britain and her colonies 92 * (including what is now the United States), did not switch to the Gregorian 93 * calendar until 1752, when Wednesday 2nd September in the Julian calendar dawned 94 * as Thursday the 14th in the Gregorian. 95 * 96 * The Gregorian calendar is a minor correction to the Julian. In the Julian 97 * calendar every fourth year is a leap year in which February has 29, not 28 days, 98 * but in the Gregorian, years divisible by 100 are not leap years unless they are 99 * also divisible by 400. How prescient was Pope Gregory! Whatever the problems of 100 * Y2K, they won't include sloppy programming which assumes every year divisible by 101 * 4 is a leap year since 2000, unlike the previous and subsequent years divisible 102 * by 100, is a leap year. As in the Julian calendar, days are considered to begin 103 * at midnight. 104 * 105 * The average length of a year in the Gregorian calendar is 365.2425 days compared 106 * to the actual solar tropical year (time from equinox to equinox) of 365.24219878 107 * days, so the calendar accumulates one day of error with respect to the solar year 108 * about every 3300 years. As a purely solar calendar, no attempt is made to 109 * synchronise the start of months to the phases of the Moon. 110 * 111 * date -- Format a local time/date 112 * string date ( string format, int timestamp); 113 * 114 * Returns a string formatted according to the given format string using the given 115 * integer timestamp or the current local time if no timestamp is given. In 116 * otherwords, timestamp is optional and defaults to the value of time(). 117 * 118 * Example: 119 * <code> 120 * date_default_timezone_set('UTC'); 121 * $time = time(); 122 * 123 * echo date('l dS F Y h:i:s A', $time); 124 * echo '<br /><br />'; 125 * 126 * include('./Arabic.php'); 127 * $Ar = new Arabic('ArDate'); 128 * 129 * echo $Ar->date('l dS F Y h:i:s A', $time); 130 * echo '<br /><br />'; 131 * 132 * $Ar->ArDate->setMode(2); 133 * echo $Ar->date('l dS F Y h:i:s A', $time); 134 * echo '<br /><br />'; 135 * 136 * $Ar->ArDate->setMode(3); 137 * echo $Ar->date('l dS F Y h:i:s A', $time); 138 * echo '<br /><br />'; 139 * 140 * $Ar->ArDate->setMode(4); 141 * echo $Ar->date('l dS F Y h:i:s A', $time); 142 * </code> 143 * 144 * @category Text 145 * @package Arabic 146 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 147 * @copyright 2009 Khaled Al-Shamaa 148 * 149 * @license LGPL <http://www.gnu.org/licenses/lgpl.txt> 150 * @link http://www.ar-php.org 151 */ 271 152 272 } else { 273 $str = date($format, $timestamp); 274 $str = $this->en2ar($str); 275 } 276 277 return $str; 278 } 279 280 /** 281 * @return String Date/time string using Arabic terms 282 * @param String $str Date/time string using English terms 283 * @desc Translate English date/time terms into Arabic langauge 284 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 285 */ 286 private function en2ar($str) 287 { 288 $patterns = array(); 289 $replacements = array(); 290 291 $str = strtolower($str); 292 293 array_push($patterns, 'saturday'); 294 array_push($replacements, 'السبت'); 295 array_push($patterns, 'sunday'); 296 array_push($replacements, 'الأحد'); 297 array_push($patterns, 'monday'); 298 array_push($replacements, 'الاثنين'); 299 array_push($patterns, 'tuesday'); 300 array_push($replacements, 'الثلاثاء'); 301 array_push($patterns, 'wednesday'); 302 array_push($replacements, 'الأربعاء'); 303 array_push($patterns, 'thursday'); 304 array_push($replacements, 'الخميس'); 305 array_push($patterns, 'friday'); 306 array_push($replacements, 'الجمعة'); 307 308 if ($this->mode == 2) { 309 array_push($patterns, 'january'); 310 array_push($replacements, 'كانون ثاني'); 311 array_push($patterns, 'february'); 312 array_push($replacements, 'شباط'); 313 array_push($patterns, 'march'); 314 array_push($replacements, 'آذار'); 315 array_push($patterns, 'april'); 316 array_push($replacements, 'نيسان'); 317 array_push($patterns, 'may'); 318 array_push($replacements, 'أيار'); 319 array_push($patterns, 'june'); 320 array_push($replacements, 'حزيران'); 321 array_push($patterns, 'july'); 322 array_push($replacements, 'تموز'); 323 array_push($patterns, 'august'); 324 array_push($replacements, 'آب'); 325 array_push($patterns, 'september'); 326 array_push($replacements, 'أيلول'); 327 array_push($patterns, 'october'); 328 array_push($replacements, 'تشرين أول'); 329 array_push($patterns, 'november'); 330 array_push($replacements, 'تشرين ثاني'); 331 array_push($patterns, 'december'); 332 array_push($replacements, 'كانون أول'); 333 } elseif ($this->mode == 3) { 334 array_push($patterns, 'january'); 335 array_push($replacements, 'يناير'); 336 array_push($patterns, 'february'); 337 array_push($replacements, 'فبراير'); 338 array_push($patterns, 'march'); 339 array_push($replacements, 'مارس'); 340 array_push($patterns, 'april'); 341 array_push($replacements, 'أبريل'); 342 array_push($patterns, 'may'); 343 array_push($replacements, 'مايو'); 344 array_push($patterns, 'june'); 345 array_push($replacements, 'يونيو'); 346 array_push($patterns, 'july'); 347 array_push($replacements, 'يوليو'); 348 array_push($patterns, 'august'); 349 array_push($replacements, 'أغسطس'); 350 array_push($patterns, 'september'); 351 array_push($replacements, 'سبتمبر'); 352 array_push($patterns, 'october'); 353 array_push($replacements, 'أكتوبر'); 354 array_push($patterns, 'november'); 355 array_push($replacements, 'نوفمبر'); 356 array_push($patterns, 'december'); 357 array_push($replacements, 'ديسمبر'); 358 } elseif ($this->mode == 4) { 359 array_push($patterns, 'january'); 360 array_push($replacements, 'كانون ثاني/يناير'); 361 array_push($patterns, 'february'); 362 array_push($replacements, 'شباط/فبراير'); 363 array_push($patterns, 'march'); 364 array_push($replacements, 'آذار/مارس'); 365 array_push($patterns, 'april'); 366 array_push($replacements, 'نيسان/أبريل'); 367 array_push($patterns, 'may'); 368 array_push($replacements, 'أيار/مايو'); 369 array_push($patterns, 'june'); 370 array_push($replacements, 'حزيران/يونيو'); 371 array_push($patterns, 'july'); 372 array_push($replacements, 'تموز/يوليو'); 373 array_push($patterns, 'august'); 374 array_push($replacements, 'آب/أغسطس'); 375 array_push($patterns, 'september'); 376 array_push($replacements, 'أيلول/سبتمبر'); 377 array_push($patterns, 'october'); 378 array_push($replacements, 'تشرين أول/أكتوبر'); 379 array_push($patterns, 'november'); 380 array_push($replacements, 'تشرين ثاني/نوفمبر'); 381 array_push($patterns, 'december'); 382 array_push($replacements, 'كانون أول/ديسمبر'); 383 } elseif ($this->mode == 5) { 384 array_push($patterns, 'january'); 385 array_push($replacements, 'أي النار'); 386 array_push($patterns, 'february'); 387 array_push($replacements, 'النوار'); 388 array_push($patterns, 'march'); 389 array_push($replacements, 'الربيع'); 390 array_push($patterns, 'april'); 391 array_push($replacements, 'الطير'); 392 array_push($patterns, 'may'); 393 array_push($replacements, 'الماء'); 394 array_push($patterns, 'june'); 395 array_push($replacements, 'الصيف'); 396 array_push($patterns, 'july'); 397 array_push($replacements, 'ناصر'); 398 array_push($patterns, 'august'); 399 array_push($replacements, 'هانيبال'); 400 array_push($patterns, 'september'); 401 array_push($replacements, 'الفاتح'); 402 array_push($patterns, 'october'); 403 array_push($replacements, 'التمور'); 404 array_push($patterns, 'november'); 405 array_push($replacements, 'الحرث'); 406 array_push($patterns, 'december'); 407 array_push($replacements, 'الكانون'); 408 } 409 410 array_push($patterns, 'sat'); 411 array_push($replacements, 'السبت'); 412 array_push($patterns, 'sun'); 413 array_push($replacements, 'الأحد'); 414 array_push($patterns, 'mon'); 415 array_push($replacements, 'الاثنين'); 416 array_push($patterns, 'tue'); 417 array_push($replacements, 'الثلاثاء'); 418 array_push($patterns, 'wed'); 419 array_push($replacements, 'الأربعاء'); 420 array_push($patterns, 'thu'); 421 array_push($replacements, 'الخميس'); 422 array_push($patterns, 'fri'); 423 array_push($replacements, 'الجمعة'); 424 425 if ($this->mode == 2) { 426 array_push($patterns, 'jan'); 427 array_push($replacements, 'كانون ثاني'); 428 array_push($patterns, 'feb'); 429 array_push($replacements, 'شباط'); 430 array_push($patterns, 'mar'); 431 array_push($replacements, 'آذار'); 432 array_push($patterns, 'apr'); 433 array_push($replacements, 'نيسان'); 434 array_push($patterns, 'may'); 435 array_push($replacements, 'أيار'); 436 array_push($patterns, 'jun'); 437 array_push($replacements, 'حزيران'); 438 array_push($patterns, 'jul'); 439 array_push($replacements, 'تموز'); 440 array_push($patterns, 'aug'); 441 array_push($replacements, 'آب'); 442 array_push($patterns, 'sep'); 443 array_push($replacements, 'أيلول'); 444 array_push($patterns, 'oct'); 445 array_push($replacements, 'تشرين أول'); 446 array_push($patterns, 'nov'); 447 array_push($replacements, 'تشرين ثاني'); 448 array_push($patterns, 'dec'); 449 array_push($replacements, 'كانون أول'); 450 } elseif ($this->mode == 3) { 451 array_push($patterns, 'jan'); 452 array_push($replacements, 'يناير'); 453 array_push($patterns, 'feb'); 454 array_push($replacements, 'فبراير'); 455 array_push($patterns, 'mar'); 456 array_push($replacements, 'مارس'); 457 array_push($patterns, 'apr'); 458 array_push($replacements, 'أبريل'); 459 array_push($patterns, 'may'); 460 array_push($replacements, 'مايو'); 461 array_push($patterns, 'jun'); 462 array_push($replacements, 'يونيو'); 463 array_push($patterns, 'jul'); 464 array_push($replacements, 'يوليو'); 465 array_push($patterns, 'aug'); 466 array_push($replacements, 'أغسطس'); 467 array_push($patterns, 'sep'); 468 array_push($replacements, 'سبتمبر'); 469 array_push($patterns, 'oct'); 470 array_push($replacements, 'أكتوبر'); 471 array_push($patterns, 'nov'); 472 array_push($replacements, 'نوفمبر'); 473 array_push($patterns, 'dec'); 474 array_push($replacements, 'ديسمبر'); 475 } elseif ($this->mode == 4) { 476 array_push($patterns, 'jan'); 477 array_push($replacements, 'كانون ثاني/يناير'); 478 array_push($patterns, 'feb'); 479 array_push($replacements, 'شباط/فبراير'); 480 array_push($patterns, 'mar'); 481 array_push($replacements, 'آذار/مارس'); 482 array_push($patterns, 'apr'); 483 array_push($replacements, 'نيسان/أبريل'); 484 array_push($patterns, 'may'); 485 array_push($replacements, 'أيار/مايو'); 486 array_push($patterns, 'jun'); 487 array_push($replacements, 'حزيران/يونيو'); 488 array_push($patterns, 'jul'); 489 array_push($replacements, 'تموز/يوليو'); 490 array_push($patterns, 'aug'); 491 array_push($replacements, 'آب/أغسطس'); 492 array_push($patterns, 'sep'); 493 array_push($replacements, 'أيلول/سبتمبر'); 494 array_push($patterns, 'oct'); 495 array_push($replacements, 'تشرين أول/أكتوبر'); 496 array_push($patterns, 'nov'); 497 array_push($replacements, 'تشرين ثاني/نوفمبر'); 498 array_push($patterns, 'dec'); 499 array_push($replacements, 'كانون أول/ديسمبر'); 500 } elseif ($this->mode == 5) { 501 array_push($patterns, 'jan'); 502 array_push($replacements, 'أي النار'); 503 array_push($patterns, 'feb'); 504 array_push($replacements, 'النوار'); 505 array_push($patterns, 'mar'); 506 array_push($replacements, 'الربيع'); 507 array_push($patterns, 'apr'); 508 array_push($replacements, 'الطير'); 509 array_push($patterns, 'may'); 510 array_push($replacements, 'الماء'); 511 array_push($patterns, 'jun'); 512 array_push($replacements, 'الصيف'); 513 array_push($patterns, 'jul'); 514 array_push($replacements, 'ناصر'); 515 array_push($patterns, 'aug'); 516 array_push($replacements, 'هانيبال'); 517 array_push($patterns, 'sep'); 518 array_push($replacements, 'الفاتح'); 519 array_push($patterns, 'oct'); 520 array_push($replacements, 'التمور'); 521 array_push($patterns, 'nov'); 522 array_push($replacements, 'الحرث'); 523 array_push($patterns, 'dec'); 524 array_push($replacements, 'الكانون'); 525 } 526 527 array_push($patterns, 'am'); 528 array_push($replacements, 'صباحاً'); 529 array_push($patterns, 'pm'); 530 array_push($replacements, 'مساءً'); 531 532 array_push($patterns, 'st'); 533 array_push($replacements, ''); 534 array_push($patterns, 'nd'); 535 array_push($replacements, ''); 536 array_push($patterns, 'rd'); 537 array_push($replacements, ''); 538 array_push($patterns, 'th'); 539 array_push($replacements, ''); 540 541 $str = str_replace($patterns, $replacements, $str); 542 543 return $str; 544 } 545 546 /** 547 * @return Array Higri date [int Year, int Month, int Day](Islamic calendar) 548 * @param Integer $Y Year Gregorian year 549 * @param Integer $M Month Gregorian month 550 * @param Integer $D Day Gregorian day 551 * @desc Convert given Gregorian date into Higri date 552 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 553 */ 554 private function hj_convert($Y, $M, $D) 555 { 556 // To get these functions to work, you have to compile PHP with --enable-calendar 557 // http://www.php.net/manual/en/calendar.installation.php 558 // $jd = GregorianToJD($M, $D, $Y); 559 560 $jd = $this->greg_to_jd($M, $D, $Y); 561 562 list($year, $month, $day) = $this->jd_to_islamic($jd); 563 564 return array($year, $month, $day); 565 } 566 567 /** 568 * @return Array Higri date [int Year, int Month, int Day](Islamic calendar) 569 * @param Integer jd $jd Julian day 570 * @desc Convert given Julian day into Higri date 571 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 572 */ 573 private function jd_to_islamic($jd) 574 { 575 $jd = (int)$jd + 0.5; 576 $year = ((30 * ($jd - self::$ISLAMIC_EPOCH)) + 10646) / 10631; 577 $year = (int)$year; 578 $month = min(12, ceil(($jd - (29 + $this->islamic_to_jd($year, 1, 1))) / 29.5) + 1); 579 $day = ($jd - $this->islamic_to_jd($year, $month, 1)) + 1; 580 581 return array($year, $month, $day); 582 } 583 584 /** 585 * @return Integer Julian day 586 * @param Integer $year Year Higri year 587 * @param Integer $month Month Higri month 588 * @param Integer $day Day Higri day 589 * @desc Convert given Higri date into Julian day 590 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 591 */ 592 private function islamic_to_jd($year, $month, $day) 593 { 594 return($day + ceil(29.5 * ($month - 1)) + ($year - 1) * 354 + (int)((3 + (11 * $year)) / 30) + self::$ISLAMIC_EPOCH) - 1; 595 } 596 597 /** 598 * @return Integer The julian day for the given gregorian date as an integer 599 * @param Integer $m The month as a number from 1 (for January) to 12 (for December) 600 * @param Integer $d The day as a number from 1 to 31 601 * @param Integer $y The year as a number between -4714 and 9999 602 * @desc Converts a Gregorian date to Julian Day Count 603 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 604 */ 605 private function greg_to_jd ($m, $d, $y) 606 { 607 if ($m > 2) { 608 $m = $m - 3; 609 } else { 610 $m = $m + 9; 611 $y = $y - 1; 612 } 613 614 $c = $y / 100; 615 $ya = $y - 100 * $c; 616 $jd = (146097 * $c) / 4 + (1461 * $ya) / 4 + (153 * $m + 2) / 5 + $d + 1721119; 617 618 return round($jd); 619 } 620 } 153 // New in PHP V5.3: Namespaces 154 // namespace Arabic/ArDate; 155 156 /** 157 * This PHP class is an Arabic customization for PHP date function 158 * 159 * @category Text 160 * @package Arabic 161 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 162 * @copyright 2009 Khaled Al-Shamaa 163 * 164 * @license LGPL <http://www.gnu.org/licenses/lgpl.txt> 165 * @link http://www.ar-php.org 166 */ 167 class ArDate 168 { 169 protected $_mode = 1; 170 protected static $_ISLAMIC_EPOCH = 1948439.5; 171 172 /** 173 * Setting value for $mode scalar 174 * 175 * @param integer $mode Output mode of date function where: 176 * 1) Hegri format (Islamic calendar) 177 * 2) Arabic month names used in Middle East countries 178 * 3) Arabic Transliteration of Gregorian month names 179 * 4) Both of 2 and 3 formats together 180 * 5) Libyan way 181 * 182 * @return boolean TRUE if success, or FALSE if fail 183 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 184 */ 185 public function setMode($mode = 1) 186 { 187 $flag = true; 188 189 $mode = (int) $mode; 190 191 if ($mode > 0 && $mode < 6) { 192 $this->_mode = $mode; 193 } else { 194 195 $flag = false; 196 } 197 198 return $flag; 199 } 200 201 /** 202 * Getting $mode value that refer to output mode format 203 * 1) Hegri format (Islamic calendar) 204 * 2) Arabic month names used in Middle East countries 205 * 3) Arabic Transliteration of Gregorian month names 206 * 4) Both of 2 and 3 formats together 207 * 5) Libyan way 208 * 209 * @return Integer Value of $mode properity 210 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 211 */ 212 public function getMode() 213 { 214 return $this->_mode; 215 } 216 217 /** 218 * Format a local time/date in Arabic string 219 * 220 * @param string $format Format string (same as PHP date function) 221 * @param integer $timestamp Unix timestamp 222 * @param string $outputCharset (optional) Output charset [utf-8|windows-1256|iso-8859-6] 223 * default value is NULL (use set output charset) 224 * @param integer $correction To apply correction factor (+/- 1-2) to standard hijri calendar 225 * @param object $main Main Ar-PHP object to access charset converter options 226 * 227 * @return string Format Arabic date string according to given format string 228 * using the given integer timestamp or the current local 229 * time if no timestamp is given. 230 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 231 */ 232 public function date($format, $timestamp, $outputCharset = null, $correction = 0, $main = null) 233 { 234 $timestamp = $timestamp + 3600*24*$correction; 235 236 if ($this->_mode == 1) { 237 $hj_txt_month[1] = 'محرم'; 238 $hj_txt_month[2] = 'صفر'; 239 $hj_txt_month[3] = 'ربيع الأول'; 240 $hj_txt_month[4] = 'ربيع الثاني'; 241 $hj_txt_month[5] = 'جمادى الأولى'; 242 $hj_txt_month[6] = 'جمادى الثانية'; 243 $hj_txt_month[7] = 'رجب'; 244 $hj_txt_month[8] = 'شعبان'; 245 $hj_txt_month[9] = 'رمضان'; 246 $hj_txt_month[10] = 'شوال'; 247 $hj_txt_month[11] = 'ذو القعدة'; 248 $hj_txt_month[12] = 'ذو الحجة'; 249 250 $patterns = array(); 251 $replacements = array(); 252 253 array_push($patterns, 'Y'); 254 array_push($replacements, 'x1'); 255 array_push($patterns, 'y'); 256 array_push($replacements, 'x2'); 257 array_push($patterns, 'M'); 258 array_push($replacements, 'x3'); 259 array_push($patterns, 'F'); 260 array_push($replacements, 'x3'); 261 array_push($patterns, 'n'); 262 array_push($replacements, 'x4'); 263 array_push($patterns, 'm'); 264 array_push($replacements, 'x5'); 265 array_push($patterns, 'j'); 266 array_push($replacements, 'x6'); 267 array_push($patterns, 'd'); 268 array_push($replacements, 'x7'); 269 270 $format = str_replace($patterns, $replacements, $format); 271 272 $str = date($format, $timestamp); 273 $str = $this->_en2ar($str); 274 275 list($Y, $M, $D) = split(' ', date('Y m d', $timestamp)); 276 277 list($hj_y, $hj_m, $hj_d) = $this->_hjConvert($Y, $M, $D); 278 279 $patterns = array(); 280 $replacements = array(); 281 282 array_push($patterns, 'x1'); 283 array_push($replacements, $hj_y); 284 array_push($patterns, 'x2'); 285 array_push($replacements, substr($hj_y, -2)); 286 array_push($patterns, 'x3'); 287 array_push($replacements, $hj_txt_month[$hj_m]); 288 array_push($patterns, 'x4'); 289 array_push($replacements, $hj_m); 290 array_push($patterns, 'x5'); 291 array_push($replacements, sprintf('%02d', $hj_m)); 292 array_push($patterns, 'x6'); 293 array_push($replacements, $hj_d); 294 array_push($patterns, 'x7'); 295 array_push($replacements, sprintf('%02d', $hj_d)); 296 297 $str = str_replace($patterns, $replacements, $str); 298 } elseif ($this->_mode == 5) { 299 $year = date('Y', $timestamp); 300 $year -= 632; 301 $yr = substr("$year", -2); 302 303 $format = str_replace('Y', $year, $format); 304 $format = str_replace('y', $yr, $format); 305 306 $str = date($format, $timestamp); 307 $str = $this->_en2ar($str); 308 309 } else { 310 $str = date($format, $timestamp); 311 $str = $this->_en2ar($str); 312 } 313 314 if ($main) { 315 if ($outputCharset == null) $outputCharset = $main->getOutputCharset(); 316 $str = $main->coreConvert($str, 'utf-8', $outputCharset); 317 } 318 319 return $str; 320 } 321 322 /** 323 * Translate English date/time terms into Arabic langauge 324 * 325 * @param string $str Date/time string using English terms 326 * 327 * @return string Date/time string using Arabic terms 328 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 329 */ 330 protected function _en2ar($str) 331 { 332 $patterns = array(); 333 $replacements = array(); 334 335 $str = strtolower($str); 336 337 array_push($patterns, 'saturday'); 338 array_push($replacements, 'السبت'); 339 array_push($patterns, 'sunday'); 340 array_push($replacements, 'الأحد'); 341 array_push($patterns, 'monday'); 342 array_push($replacements, 'الاثنين'); 343 array_push($patterns, 'tuesday'); 344 array_push($replacements, 'الثلاثاء'); 345 array_push($patterns, 'wednesday'); 346 array_push($replacements, 'الأربعاء'); 347 array_push($patterns, 'thursday'); 348 array_push($replacements, 'الخميس'); 349 array_push($patterns, 'friday'); 350 array_push($replacements, 'الجمعة'); 351 352 if ($this->_mode == 2) { 353 array_push($patterns, 'january'); 354 array_push($replacements, 'كانون ثاني'); 355 array_push($patterns, 'february'); 356 array_push($replacements, 'شباط'); 357 array_push($patterns, 'march'); 358 array_push($replacements, 'آذار'); 359 array_push($patterns, 'april'); 360 array_push($replacements, 'نيسان'); 361 array_push($patterns, 'may'); 362 array_push($replacements, 'أيار'); 363 array_push($patterns, 'june'); 364 array_push($replacements, 'حزيران'); 365 array_push($patterns, 'july'); 366 array_push($replacements, 'تموز'); 367 array_push($patterns, 'august'); 368 array_push($replacements, 'آب'); 369 array_push($patterns, 'september'); 370 array_push($replacements, 'أيلول'); 371 array_push($patterns, 'october'); 372 array_push($replacements, 'تشرين أول'); 373 array_push($patterns, 'november'); 374 array_push($replacements, 'تشرين ثاني'); 375 array_push($patterns, 'december'); 376 array_push($replacements, 'كانون أول'); 377 } elseif ($this->_mode == 3) { 378 array_push($patterns, 'january'); 379 array_push($replacements, 'يناير'); 380 array_push($patterns, 'february'); 381 array_push($replacements, 'فبراير'); 382 array_push($patterns, 'march'); 383 array_push($replacements, 'مارس'); 384 array_push($patterns, 'april'); 385 array_push($replacements, 'أبريل'); 386 array_push($patterns, 'may'); 387 array_push($replacements, 'مايو'); 388 array_push($patterns, 'june'); 389 array_push($replacements, 'يونيو'); 390 array_push($patterns, 'july'); 391 array_push($replacements, 'يوليو'); 392 array_push($patterns, 'august'); 393 array_push($replacements, 'أغسطس'); 394 array_push($patterns, 'september'); 395 array_push($replacements, 'سبتمبر'); 396 array_push($patterns, 'october'); 397 array_push($replacements, 'أكتوبر'); 398 array_push($patterns, 'november'); 399 array_push($replacements, 'نوفمبر'); 400 array_push($patterns, 'december'); 401 array_push($replacements, 'ديسمبر'); 402 } elseif ($this->_mode == 4) { 403 array_push($patterns, 'january'); 404 array_push($replacements, 'كانون ثاني/يناير'); 405 array_push($patterns, 'february'); 406 array_push($replacements, 'شباط/فبراير'); 407 array_push($patterns, 'march'); 408 array_push($replacements, 'آذار/مارس'); 409 array_push($patterns, 'april'); 410 array_push($replacements, 'نيسان/أبريل'); 411 array_push($patterns, 'may'); 412 array_push($replacements, 'أيار/مايو'); 413 array_push($patterns, 'june'); 414 array_push($replacements, 'حزيران/يونيو'); 415 array_push($patterns, 'july'); 416 array_push($replacements, 'تموز/يوليو'); 417 array_push($patterns, 'august'); 418 array_push($replacements, 'آب/أغسطس'); 419 array_push($patterns, 'september'); 420 array_push($replacements, 'أيلول/سبتمبر'); 421 array_push($patterns, 'october'); 422 array_push($replacements, 'تشرين أول/أكتوبر'); 423 array_push($patterns, 'november'); 424 array_push($replacements, 'تشرين ثاني/نوفمبر'); 425 array_push($patterns, 'december'); 426 array_push($replacements, 'كانون أول/ديسمبر'); 427 } elseif ($this->_mode == 5) { 428 array_push($patterns, 'january'); 429 array_push($replacements, 'أي النار'); 430 array_push($patterns, 'february'); 431 array_push($replacements, 'النوار'); 432 array_push($patterns, 'march'); 433 array_push($replacements, 'الربيع'); 434 array_push($patterns, 'april'); 435 array_push($replacements, 'الطير'); 436 array_push($patterns, 'may'); 437 array_push($replacements, 'الماء'); 438 array_push($patterns, 'june'); 439 array_push($replacements, 'الصيف'); 440 array_push($patterns, 'july'); 441 array_push($replacements, 'ناصر'); 442 array_push($patterns, 'august'); 443 array_push($replacements, 'هانيبال'); 444 array_push($patterns, 'september'); 445 array_push($replacements, 'الفاتح'); 446 array_push($patterns, 'october'); 447 array_push($replacements, 'التمور'); 448 array_push($patterns, 'november'); 449 array_push($replacements, 'الحرث'); 450 array_push($patterns, 'december'); 451 array_push($replacements, 'الكانون'); 452 } 453 454 array_push($patterns, 'sat'); 455 array_push($replacements, 'السبت'); 456 array_push($patterns, 'sun'); 457 array_push($replacements, 'الأحد'); 458 array_push($patterns, 'mon'); 459 array_push($replacements, 'الاثنين'); 460 array_push($patterns, 'tue'); 461 array_push($replacements, 'الثلاثاء'); 462 array_push($patterns, 'wed'); 463 array_push($replacements, 'الأربعاء'); 464 array_push($patterns, 'thu'); 465 array_push($replacements, 'الخميس'); 466 array_push($patterns, 'fri'); 467 array_push($replacements, 'الجمعة'); 468 469 if ($this->_mode == 2) { 470 array_push($patterns, 'jan'); 471 array_push($replacements, 'كانون ثاني'); 472 array_push($patterns, 'feb'); 473 array_push($replacements, 'شباط'); 474 array_push($patterns, 'mar'); 475 array_push($replacements, 'آذار'); 476 array_push($patterns, 'apr'); 477 array_push($replacements, 'نيسان'); 478 array_push($patterns, 'may'); 479 array_push($replacements, 'أيار'); 480 array_push($patterns, 'jun'); 481 array_push($replacements, 'حزيران'); 482 array_push($patterns, 'jul'); 483 array_push($replacements, 'تموز'); 484 array_push($patterns, 'aug'); 485 array_push($replacements, 'آب'); 486 array_push($patterns, 'sep'); 487 array_push($replacements, 'أيلول'); 488 array_push($patterns, 'oct'); 489 array_push($replacements, 'تشرين أول'); 490 array_push($patterns, 'nov'); 491 array_push($replacements, 'تشرين ثاني'); 492 array_push($patterns, 'dec'); 493 array_push($replacements, 'كانون أول'); 494 } elseif ($this->_mode == 3) { 495 array_push($patterns, 'jan'); 496 array_push($replacements, 'يناير'); 497 array_push($patterns, 'feb'); 498 array_push($replacements, 'فبراير'); 499 array_push($patterns, 'mar'); 500 array_push($replacements, 'مارس'); 501 array_push($patterns, 'apr'); 502 array_push($replacements, 'أبريل'); 503 array_push($patterns, 'may'); 504 array_push($replacements, 'مايو'); 505 array_push($patterns, 'jun'); 506 array_push($replacements, 'يونيو'); 507 array_push($patterns, 'jul'); 508 array_push($replacements, 'يوليو'); 509 array_push($patterns, 'aug'); 510 array_push($replacements, 'أغسطس'); 511 array_push($patterns, 'sep'); 512 array_push($replacements, 'سبتمبر'); 513 array_push($patterns, 'oct'); 514 array_push($replacements, 'أكتوبر'); 515 array_push($patterns, 'nov'); 516 array_push($replacements, 'نوفمبر'); 517 array_push($patterns, 'dec'); 518 array_push($replacements, 'ديسمبر'); 519 } elseif ($this->_mode == 4) { 520 array_push($patterns, 'jan'); 521 array_push($replacements, 'كانون ثاني/يناير'); 522 array_push($patterns, 'feb'); 523 array_push($replacements, 'شباط/فبراير'); 524 array_push($patterns, 'mar'); 525 array_push($replacements, 'آذار/مارس'); 526 array_push($patterns, 'apr'); 527 array_push($replacements, 'نيسان/أبريل'); 528 array_push($patterns, 'may'); 529 array_push($replacements, 'أيار/مايو'); 530 array_push($patterns, 'jun'); 531 array_push($replacements, 'حزيران/يونيو'); 532 array_push($patterns, 'jul'); 533 array_push($replacements, 'تموز/يوليو'); 534 array_push($patterns, 'aug'); 535 array_push($replacements, 'آب/أغسطس'); 536 array_push($patterns, 'sep'); 537 array_push($replacements, 'أيلول/سبتمبر'); 538 array_push($patterns, 'oct'); 539 array_push($replacements, 'تشرين أول/أكتوبر'); 540 array_push($patterns, 'nov'); 541 array_push($replacements, 'تشرين ثاني/نوفمبر'); 542 array_push($patterns, 'dec'); 543 array_push($replacements, 'كانون أول/ديسمبر'); 544 } elseif ($this->_mode == 5) { 545 array_push($patterns, 'jan'); 546 array_push($replacements, 'أي النار'); 547 array_push($patterns, 'feb'); 548 array_push($replacements, 'النوار'); 549 array_push($patterns, 'mar'); 550 array_push($replacements, 'الربيع'); 551 array_push($patterns, 'apr'); 552 array_push($replacements, 'الطير'); 553 array_push($patterns, 'may'); 554 array_push($replacements, 'الماء'); 555 array_push($patterns, 'jun'); 556 array_push($replacements, 'الصيف'); 557 array_push($patterns, 'jul'); 558 array_push($replacements, 'ناصر'); 559 array_push($patterns, 'aug'); 560 array_push($replacements, 'هانيبال'); 561 array_push($patterns, 'sep'); 562 array_push($replacements, 'الفاتح'); 563 array_push($patterns, 'oct'); 564 array_push($replacements, 'التمور'); 565 array_push($patterns, 'nov'); 566 array_push($replacements, 'الحرث'); 567 array_push($patterns, 'dec'); 568 array_push($replacements, 'الكانون'); 569 } 570 571 array_push($patterns, 'am'); 572 array_push($replacements, 'صباحاً'); 573 array_push($patterns, 'pm'); 574 array_push($replacements, 'مساءً'); 575 576 array_push($patterns, 'st'); 577 array_push($replacements, ''); 578 array_push($patterns, 'nd'); 579 array_push($replacements, ''); 580 array_push($patterns, 'rd'); 581 array_push($replacements, ''); 582 array_push($patterns, 'th'); 583 array_push($replacements, ''); 584 585 $str = str_replace($patterns, $replacements, $str); 586 587 return $str; 588 } 589 590 /** 591 * Convert given Gregorian date into Higri date 592 * 593 * @param integer $Y Year Gregorian year 594 * @param integer $M Month Gregorian month 595 * @param integer $D Day Gregorian day 596 * 597 * @return array Higri date [int Year, int Month, int Day](Islamic calendar) 598 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 599 */ 600 protected function _hjConvert($Y, $M, $D) 601 { 602 // To get these functions to work, you have to compile PHP with --enable-calendar 603 // http://www.php.net/manual/en/calendar.installation.php 604 // $jd = GregorianToJD($M, $D, $Y); 605 606 $jd = $this->_gregToJd($M, $D, $Y); 607 608 list($year, $month, $day) = $this->_jdToIslamic($jd); 609 610 return array($year, $month, $day); 611 } 612 613 /** 614 * Convert given Julian day into Higri date 615 * 616 * @param integer $jd Julian day 617 * 618 * @return array Higri date [int Year, int Month, int Day](Islamic calendar) 619 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 620 */ 621 protected function _jdToIslamic($jd) 622 { 623 $jd = (int)$jd + 0.5; 624 $year = ((30 * ($jd - self::$_ISLAMIC_EPOCH)) + 10646) / 10631; 625 $year = (int)$year; 626 $month = min(12, ceil(($jd - (29 + $this->_islamicToJd($year, 1, 1))) / 29.5) + 1); 627 $day = ($jd - $this->_islamicToJd($year, $month, 1)) + 1; 628 629 return array($year, $month, $day); 630 } 631 632 /** 633 * Convert given Higri date into Julian day 634 * 635 * @param integer $year Year Higri year 636 * @param integer $month Month Higri month 637 * @param integer $day Day Higri day 638 * 639 * @return integer Julian day 640 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 641 */ 642 protected function _islamicToJd($year, $month, $day) 643 { 644 return($day + ceil(29.5 * ($month - 1)) + ($year - 1) * 354 + (int)((3 + (11 * $year)) / 30) + self::$_ISLAMIC_EPOCH) - 1; 645 } 646 647 /** 648 * Converts a Gregorian date to Julian Day Count 649 * 650 * @param integer $m The month as a number from 1 (for January) to 12 (for December) 651 * @param integer $d The day as a number from 1 to 31 652 * @param integer $y The year as a number between -4714 and 9999 653 * 654 * @return integer The julian day for the given gregorian date as an integer 655 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 656 */ 657 protected function _gregToJd ($m, $d, $y) 658 { 659 if ($m > 2) { 660 $m = $m - 3; 661 } else { 662 $m = $m + 9; 663 $y = $y - 1; 664 } 665 666 $c = $y / 100; 667 $ya = $y - 100 * $c; 668 $jd = (146097 * $c) / 4 + (1461 * $ya) / 4 + (153 * $m + 2) / 5 + $d + 1721119; 669 670 return round($jd); 671 } 672 } 621 673 ?> -
hijri/trunk/Arabic.php
r114992 r205328 1 1 <?php 2 // ---------------------------------------------------------------------- 3 // Copyright (C) 2008 by Khaled Al-Shamaa. 4 // http://www.ar-php.org 5 // ---------------------------------------------------------------------- 6 // LICENSE 7 8 // This program is open source product; you can redistribute it and/or 9 // modify it under the terms of the GNU Lesser General Public License (LGPL) 10 // as published by the Free Software Foundation; either version 3 11 // of the License, or (at your option) any later version. 12 13 // This program is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 // GNU Lesser General Public License for more details. 17 18 // You should have received a copy of the GNU Lesser General Public License 19 // along with this program. If not, see <http://www.gnu.org/licenses/>. 20 // ---------------------------------------------------------------------- 21 // Class Name: Arabic Language Tools 22 // Filename: Arabic.php 23 // Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org> 24 // Purpose: Set of PHP5 / UTF-8 Classes developed to enhance Arabic web 25 // applications by providing set of tools includes stem-based searching, 26 // translitiration, soundex, Hijri calendar, charset detection and 27 // converter, spell numbers, keyboard language, Muslim prayer time, 28 // auto-summarization, and more... 29 // ---------------------------------------------------------------------- 30 31 /** 32 * @copyright Khaled Al-Shamaa 2008 33 * @link http://www.ar-php.org 34 * @author Khaled Al-Shamaa <khaled@ar-php.org> 35 * @desc Set of PHP5 / UTF-8 Classes developed to enhance Arabic web 36 * applications by providing set of tools includes stem-based searching, 37 * translitiration, soundex, Hijri calendar, charset detection and 38 * converter, spell numbers, keyboard language, Muslim prayer time, 39 * auto-summarization, and more... 40 * @package Arabic 41 * 42 * @version 1.8 released in Feb 15, 2009 43 * 44 * @license LGPL 45 */ 46 47 //error_reporting(E_STRICT); 48 49 class ArabicException extends Exception 50 { 51 public function __construct($message, $code=0) 52 { 53 parent::__construct($message, $code); 54 } 55 } 56 57 function arabic_error_handler($errno, $errstr, $errfile, $errline) 58 { 59 if($errfile == __FILE__ || file_exists(dirname(__FILE__).'/sub/'.basename($errfile))) 60 { 61 $msg = '<b>Arabic Class Exception:</b> '; 62 $msg .= $errstr; 63 $msg .= " in <b>$errfile</b>"; 64 $msg .= " on line <b>$errline</b><br />"; 65 66 throw new ArabicException($msg, $errno); 67 } 68 69 // If the function returns false then the normal error handler continues 70 return false; 71 } 72 73 set_error_handler('arabic_error_handler'); 74 75 $path = strtr(__FILE__, '\\', '/'); 76 $path = substr($path, 0, strrpos($path, '/')); 77 78 function __autoload($class_name) { 79 require $class_name . '.class.php'; 80 } 81 82 class Arabic 83 { 84 private $inputCharset = 'utf-8'; 85 private $outputCharset = 'utf-8'; 86 87 /** 88 * Muslim Prayer Times Object 89 * 90 * The five Islamic prayers are named Fajr, Zuhr, Asr, Maghrib 91 * and Isha. The timing of these five prayers varies from place 92 * to place and from day to day. It is obligatory for Muslims 93 * to perform these prayers at the correct time 94 */ 95 public $Salat; 96 97 /** 98 * Arabic Charset Converter Object 99 * 100 * Convert a given Arabic string from one Arabic character set to another, 101 * those available character sets includes the most popular three: 102 * Windows-1256, ISO 8859-6, and UTF-8 103 */ 104 public $ArCharsetC; 105 106 /** 107 * Detect Arabic String Character Set Object 108 * 109 * This class will return Arabic character set that used for 110 * a given Arabic string passing into this class, those available 111 * character sets that can be detected by this class includes 112 * the most popular three: Windows-1256, ISO 8859-6, and UTF-8 113 */ 114 public $ArCharsetD; 115 116 /** 117 * Arabic Date Object 118 * 119 * Arabic customization for PHP date function 120 */ 121 public $ArDate; 122 123 /** 124 * English-Arabic Transliteration Object 125 * 126 * Transliterate English words into Arabic by render them 127 * in the orthography of the Arabic language 128 */ 129 public $ArTransliteration; 130 131 /** 132 * Arabic-English Transliteration Object 133 * 134 * Transliterate Arabic words into English by render them 135 * in the orthography of the English language 136 */ 137 public $EnTransliteration; 138 139 /** 140 * Identify Arabic Text Segments Object 141 * 142 * This class will identify Arabic text in a given UTF-8 multi 143 * language document, it will return array of start and end 144 * positions for Arabic text segments 145 */ 146 public $ArIdentifier; 147 148 /** 149 * Arabic Gender Guesser Object 150 * 151 * This class attempts to guess the gender of Arabic names 152 */ 153 public $ArGender; 154 155 /** 156 * Spell numbers in the Arabic idiom Object 157 * 158 * Spell numbers in the Arabic idiom 159 */ 160 public $ArNumbers; 161 162 /** 163 * Arabic Keyboard Swapping Language Object 164 * 165 * Convert keyboard language programmatically (English - Arabic) 166 */ 167 public $ArKeySwap; 168 169 /** 170 * Arabic Glyphs is a simple class to render Arabic text Object 171 * 172 * This class takes Arabic text (encoded in Windows-1256 character 173 * set) as input and performs Arabic glyph joining on it and outputs 174 * a UTF-8 hexadecimals stream that is no longer logically arranged 175 * but in a visual order which gives readable results when formatted 176 * with a simple Unicode rendering just like GD and UFPDF libraries 177 * that does not handle basic connecting glyphs of Arabic language 178 * yet but simply outputs all stand alone glyphs in left-to-right order 179 */ 180 public $ArGlyphs; 181 182 /** 183 * Arabic Soundex Object 184 * 185 * Arabic soundex algorithm takes Arabic word as an input 186 * and produces a character string which identifies a set words 187 * that are (roughly) phonetically alike 188 */ 189 public $ArSoundex; 190 191 /** 192 * Tagging Arabic Word Object 193 * 194 * Arabic grammarians describe Arabic as being derived from 195 * three main categories: noun, verb and particle. This class 196 * built to recognize the class of a given Arabic word 197 */ 198 public $ArWordTag; 199 200 /** 201 * Arabic Auto Summarize Object 202 * 203 * Automatic keyphrase extraction to provide a quick mini-summary 204 * for a long Arabic document 205 */ 206 public $ArAutoSummarize; 207 208 /** 209 * Arabic Queary Object 210 * 211 * Build WHERE condition for SQL statement using MySQL REGEXP and 212 * Arabic lexical rules 213 */ 214 public $ArQuery; 215 216 /** 217 * Arabic Maketime Object 218 * 219 * Arabic customization for PHP mktime function 220 */ 221 public $ArMktime; 222 223 /** 224 * Arabic StrToTime Class 225 * 226 * PHP class to parse about any Arabic textual datetime description into a Unix timestamp 227 */ 228 public $ArStrToTime; 229 230 /** 231 * Arabic Text Standardize Class 232 * 233 * PHP class to standardize Arabic text 234 */ 235 public $ArStandardization; 236 237 /** 238 * Arabic Compress String Class 239 * 240 * Compress string using Huffman-like coding 241 */ 242 public $ArCompressStr; 243 244 /** 245 * @desc Load all objects 246 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 247 */ 248 public function __construct($mode = 'All') 249 { 250 switch ($mode) { 251 case 'All': 252 $this->Salat = new Salat(); 253 $this->ArCharsetD = new ArCharsetD(); 254 $this->ArDate = new ArDate(); 255 $this->ArIdentifier = new ArIdentifier(); 256 $this->ArNumbers = new ArNumbers(); 257 $this->ArGlyphs = new ArGlyphs(); 258 $this->ArSoundex = new ArSoundex(); 259 $this->ArAutoSummarize = new ArAutoSummarize(); 260 $this->ArQuery = new ArQuery(); 261 $this->ArMktime = new ArMktime(); 262 $this->ArStandardization = new ArStandardization(); 263 break; 264 case 'Date': 265 $this->ArDate = new ArDate(); 266 $this->ArMktime = new ArMktime(); 267 break; 268 case 'DB': 269 $this->ArQuery = new ArQuery(); 270 $this->ArSoundex = new ArSoundex(); 271 break; 272 case 'Islamic': 273 $this->Salat = new Salat(); 274 break; 275 case 'Input': 276 $this->ArCharsetD = new ArCharsetD(); 277 $this->ArIdentifier = new ArIdentifier(); 278 break; 279 case 'Output': 280 $this->ArGlyphs = new ArGlyphs(); 281 $this->ArNumbers = new ArNumbers(); 282 $this->ArStandardization = new ArStandardization(); 283 break; 284 case 'Mining': 285 $this->ArAutoSummarize = new ArAutoSummarize(); 286 break; 287 default: 288 if ($mode != 'ArCharsetC') { 289 $this->load($mode); 290 } 291 } 292 } 293 294 // instantiation of objects at runtime 295 public function load($class) 296 { 297 $this->{$class} = new $class(); 298 } 299 300 /** 301 * @return TRUE if success, or FALSE if fail 302 * @param String $charset Input charset [utf-8|windows-1256|iso-8859-6] 303 * @desc Set charset used in class input Arabic strings 304 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 305 */ 306 public function setInputCharset($charset) 307 { 308 $flag = true; 309 310 $charset = strtolower($charset); 311 312 if (in_array($charset, array('utf-8', 'windows-1256', 'iso-8859-6'))) { 313 $this->inputCharset = $charset; 314 } else { 315 $flag = false; 316 } 317 318 return $flag; 319 } 320 321 /** 322 * @return TRUE if success, or FALSE if fail 323 * @param String $charset Output charset [utf-8|windows-1256|iso-8859-6] 324 * @desc Set charset used in class output Arabic strings 325 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 326 */ 327 public function setOutputCharset($charset) 328 { 329 $flag = true; 330 331 $charset = strtolower($charset); 332 333 if (in_array($charset, array('utf-8', 'windows-1256', 'iso-8859-6'))) { 334 $this->outputCharset = $charset; 335 } else { 336 $flag = false; 337 } 338 339 return $flag; 340 } 341 342 /** 343 * @return String return current setting for class input Arabic charset 344 * @desc Get the charset used in the input Arabic strings 345 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 346 */ 347 public function getInputCharset() 348 { 349 return $this->inputCharset; 350 } 351 352 /** 353 * @return String return current setting for class output Arabic charset 354 * @desc Get the charset used in the output Arabic strings 355 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 356 */ 357 public function getOutputCharset() 358 { 359 return $this->outputCharset; 360 } 361 362 private function _convert($str, $inputCharset, $outputCharset) 363 { 364 if ($inputCharset != $outputCharset) { 365 $str = iconv($inputCharset, $outputCharset, $str); 366 } 367 return $str; 368 } 369 370 /** 371 * @return String Arabic string in the format set by method setOutputCharset 372 * @param String $str Arabic string in the format set by setInputCharset 373 * @desc Convert Arabic string from one format to another 374 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 375 */ 376 public function convert($str) 377 { 378 $str = $this->_convert($str, $this->inputCharset, $this->outputCharset); 379 380 return $str; 381 } 382 383 /** 384 * @return String Format Arabic date string according to given format string 385 * using the given integer timestamp or the current local 386 * time if no timestamp is given (using the format set by 387 * method setOutputCharset). 388 * @param String $format Format string (same as PHP date function) 389 * @param Integer $timestamp Unix timestamp (default is current local time) 390 * @desc Format a local time/date in Arabic string 391 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 392 */ 393 public function date($format, $timestamp = time) 394 { 395 $str = $this->ArDate->date($format, $timestamp); 396 397 $str = $this->_convert($str, 'utf-8', $this->outputCharset); 398 399 return $str; 400 } 401 402 /** 403 * @return String Out of vocabulary English string in Arabic characters using 404 * the format set by method setOutputCharset 405 * @param String $string English string you want to Transliterate 406 * @desc Transliterate English string into Arabic by render them in 407 * the orthography of the Arabic language 408 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 409 */ 410 public function en2ar($string) 411 { 412 $str = ArTransliteration::en2ar($string); 413 414 $str = $this->_convert($str, 'utf-8', $this->outputCharset); 415 416 return $str; 417 } 418 419 /** 420 * @return String Out of vocabulary Arabic string in English characters using 421 * the format set by method setInputCharset 422 * @param String $string Arabic string you want to Transliterate 423 * @desc Transliterate Arabic string into English by render them in 424 * the orthography of the English language 425 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 426 */ 427 public function ar2en($string) 428 { 429 $string = $this->_convert($string, $this->inputCharset, 'utf-8'); 430 431 $str = EnTransliteration::ar2en($string); 432 433 return $str; 434 } 435 436 /** 437 * @return Boolean Return true if input Arabic word is feminine 438 * @param String $string Arabic word you want to check if it is feminine 439 * @desc Check if Arabic word is feminine 440 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 441 */ 442 public function isFemale($string) 443 { 444 $string = $this->_convert($string, $this->inputCharset, 'windows-1256'); 445 446 $bool = ArGender::isFemale($string); 447 448 return $bool; 449 } 450 451 /** 452 * @return String The Arabic idiom that spells inserted number 453 * @param Integer $number The number you want to spell in Arabic idiom 454 * @desc Spell integer number in Arabic idiom 455 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 456 */ 457 public function int2str($number) 458 { 459 $str = $this->ArNumbers->int2str($number); 460 461 $str = $this->_convert($str, 'utf-8', $this->outputCharset); 462 463 return $str; 464 } 465 466 /** 467 * @return String Normal English string 468 * @param String $string Odd Arabic string 469 * @desc Make conversion to swap that odd Arabic text by original 470 * English sentence you meant when you type on your keyboard 471 * (if keyboard language was not correct) 472 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 473 */ 474 public function swap_ae($string) 475 { 476 $string = $this->_convert($string, $this->inputCharset, 'utf-8'); 477 478 $str = ArKeySwap::swap_ae($string); 479 480 return $str; 481 } 482 483 /** 484 * @return String Normal Arabic string 485 * @param String $string Odd English string 486 * @desc Make conversion to swap that odd English text by original 487 * Arabic sentence you meant when you type on your keyboard 488 * (if keyboard language was not correct) 489 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 490 */ 491 public function swap_ea($string) 492 { 493 $str = ArKeySwap::swap_ea($string); 494 495 $str = $this->_convert($str, 'utf-8', $this->outputCharset); 496 497 return $str; 498 } 499 500 /** 501 * @return Integer Number of lines for a given Arabic string in A4 page size 502 * @param String $str Arabic string you would like to split it into lines 503 * @param Integer $font Font size 504 * @desc Calculate the lines number of given Arabic text and font 505 * size that will fit in A4 page size 506 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 507 */ 508 public function a4_lines($string, $font) 509 { 510 $string = $this->_convert($string, $this->inputCharset, 'windows-1256'); 511 512 $int = $this->ArGlyphs->a4_lines($string, $font); 513 514 return $int; 515 } 516 517 /** 518 * @return String Arabic glyph joining in UTF-8 hexadecimals stream (take 519 * care of whole document including English sections as well 520 * as numbers and arcs etc...) 521 * @param String $str Arabic string 522 * @param Integer $max_chars Max number of chars you can fit in one line 523 * @param Boolean $hindo If true use Hindo digits else use Arabic digits 524 * @desc Convert Arabic string into glyph 525 * joining in UTF-8 hexadecimals stream (take care of whole 526 * the document including English sections as well as numbers 527 * and arcs etc...) 528 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 529 */ 530 public function utf8Glyphs($string, $max_chars = 50, $hindo = true) 531 { 532 $string = $this->_convert($string, $this->inputCharset, 'windows-1256'); 533 534 $str = $this->ArGlyphs->convert($string, $max_chars, $hindo); 535 536 return $str; 537 } 538 539 /** 540 * @return String Soundex value for a given Arabic word 541 * @param String $word Arabic word you want to calculate its soundex 542 * @desc Arabic soundex algorithm takes Arabic word as an input and 543 * produces a character string which identifies a set words 544 * that are (roughly) phonetically alike. 545 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 546 */ 547 public function soundex($word) 548 { 549 $word = $this->_convert($word, $this->inputCharset, 'windows-1256'); 550 551 $str = $this->ArSoundex->soundex($word); 552 553 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 554 555 return $str; 556 } 557 558 /** 559 * @return TRUE if given word is Arabic noun 560 * @param String $word Word you want to check if it is noun (windows-1256) 561 * @param String $word_befor The word before word you want to check 562 * @desc Check if given rabic word is noun or not 563 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 564 */ 565 public function isNoun($word, $word_befor) 566 { 567 $word = $this->_convert($word, $this->inputCharset, 'windows-1256'); 568 $word_befor = $this->_convert($word_befor, $this->inputCharset, 'windows-1256'); 569 570 $bool = ArWordTag::isNoun($word, $word_befor); 571 572 return $bool; 573 } 574 575 /** 576 * @return Array Two dimension array where item[i][0] represent the word i 577 * in the given string, and item[i][1] is 1 if that word is 578 * noun and 0 if it is not 579 * @param String $str Arabic string you want to tag all its words 580 * @desc Tag all words in a given Arabic string if they are nouns 581 * or not 582 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 583 */ 584 public function tagText($str) 585 { 586 $str = $this->_convert($str, $this->inputCharset, 'windows-1256'); 587 588 $arr = ArWordTag::tagText($str); 589 590 $max = count($arr); 591 for ($i = 0; $i < $max; $i++) { 592 $arr[$i][0] = $this->_convert($arr[$i][0], 'windows-1256', $this->outputCharset); 593 } 594 595 return $arr; 596 } 597 598 /** 599 * @return String Arabic string in HTML format where all nouns highlighted 600 * @param String $str Arabic string you want to highlighted all its nouns 601 * @param String $bg Background color of the highlight Arabic text 602 * @desc Highlighted all nouns in a given Arabic string 603 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 604 */ 605 public function highlightText($str, $bg = '#FFEEAA') 606 { 607 $str = $this->_convert($str, $this->inputCharset, 'windows-1256'); 608 609 $str = ArWordTag::highlightText($str, $bg); 610 611 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 612 613 return $str; 614 } 615 616 /** 617 * @return String Output summary requested 618 * @param String $str Input Arabic document as a string 619 * @param Integer $int Number of sentences required in output summary 620 * @param String $keywords List of keywords higlited by search process 621 * @desc Summarize input Arabic string (document content) into 622 * specific number of sentences in the output 623 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 624 */ 625 public function doSummarize($str, $int, $keywords) 626 { 627 $str = $this->_convert($str, $this->inputCharset, 'windows-1256'); 628 629 $str = $this->ArAutoSummarize->doSummarize($str, $int, $keywords); 630 631 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 632 633 return $str; 634 } 635 636 /** 637 * @return String Output summary requested 638 * @param String $str Input Arabic document as a string 639 * @param Integer $rate Rate of output summary sentence number as percentage 640 * of the input Arabic string (document content) 641 * @param String $keywords List of keywords higlited by search process 642 * @desc Summarize percentage of the input Arabic string 643 * (document content) into output 644 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 645 */ 646 public function doRateSummarize($str, $rate, $keywords) 647 { 648 $str = $this->_convert($str, $this->inputCharset, 'windows-1256'); 649 650 $str = $this->ArAutoSummarize->doRateSummarize($str, $rate, $keywords); 651 652 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 653 654 return $str; 655 } 656 657 /** 658 * @return String Output highlighted key sentences summary (using CSS) 659 * @param String $str Input Arabic document as a string 660 * @param Integer $int Number of key sentences required to be highlighted in 661 * the input string (document content) 662 * @param String $keywords List of keywords higlited by search process 663 * @param String $bg Background color of the highlight summary text 664 * @desc Highlight key sentences (summary) of the input 665 * string (document content) using CSS and send the 666 * result back as an output 667 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 668 */ 669 public function highlightSummary($str, $int, $keywords, $bg = '#FFEEAA') 670 { 671 $str = $this->_convert($str, $this->inputCharset, 'windows-1256'); 672 673 $str = $this->ArAutoSummarize->highlightSummary($str, $int, $keywords, $bg); 674 675 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 676 677 return $str; 678 } 679 680 /** 681 * @return String Output highlighted key sentences summary (using CSS) 682 * @param String $str Input Arabic document as a string 683 * @param Integer $rate Rate of highlighted key sentences summary number as 684 * percentage of the input Arabic string 685 * (document content) 686 * @param String $keywords List of keywords higlited by search process 687 * @param String $bg Background color of the highlight summary text 688 * @desc Highlight key sentences (summary) as percentage 689 * of the input string (document content) using CSS and send 690 * the result back as an output 691 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 692 */ 693 public function highlightRateSummary($str, $rate, $keywords, $bg = '#FFEEAA') 694 { 695 $str = $this->_convert($str, $this->inputCharset, 'windows-1256'); 696 697 $str = $this->ArAutoSummarize->highlightRateSummary($str, $rate, $keywords, $bg); 698 699 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 700 701 return $str; 702 } 703 704 /** 705 * @return String List of the keywords extracting from input Arabic string 706 * (document content) 707 * @param String $str Input Arabic document as a string 708 * @param Integer $int Number of keywords required to be extracting from 709 * input string (document content) 710 * @desc Extract keywords from a given Arabic string 711 * (document content) 712 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 713 */ 714 public function getMetaKeywords($str, $int) 715 { 716 $str = $this->_convert($str, $this->inputCharset, 'windows-1256'); 717 718 $str = $this->ArAutoSummarize->getMetaKeywords($str, $int); 719 720 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 721 722 return $str; 723 } 724 725 /** 726 * @return String The WHERE section in SQL statement 727 * (MySQL database engine format) 728 * @param String $arg String that user search for in the database table 729 * @desc Build WHERE section of the SQL statement using 730 * defind lex's rules, search mode [AND | OR], and 731 * handle also phrases (inclosed by "") using normal 732 * LIKE condition to match it as it is. 733 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 734 */ 735 public function getWhereCondition($arg) 736 { 737 $arg = $this->_convert($arg, $this->inputCharset, 'windows-1256'); 738 739 $str = $this->ArQuery->getWhereCondition($arg); 740 741 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 742 743 return $str; 744 } 745 746 /** 747 * @return String sub SQL ORDER BY section 748 * @param String $arg String that user search for in the database table 749 * @desc Get more relevant order by section related to the user search keywords 750 * @author Saleh AlMatrafe <saleh@saleh.cc> 751 */ 752 public function getOrderBy($arg) 753 { 754 $arg = $this->_convert($arg, $this->inputCharset, 'windows-1256'); 755 756 $str = $this->ArQuery->getOrderBy($arg); 757 758 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 759 760 return $str; 761 } 762 763 /** 764 * @return String list of most possible Arabic lexical forms for given keywords 765 * @param String $arg String that user search for 766 * @desc Get most possible Arabic lexical forms of user search keywords 767 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 768 */ 769 public function allForms($arg) 770 { 771 $arg = $this->_convert($arg, $this->inputCharset, 'windows-1256'); 772 773 $str = $this->ArQuery->allForms($arg); 774 775 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 776 777 return $str; 778 } 779 780 /** 781 * @return Integer Returns a timestamp on success, FALSE otherwise 782 * @param String $text The string to parse, according to the GNU » Date Input Formats syntax (in Arabic). 783 * @param Integer $now The timestamp used to calculate the returned value. 784 * @desc This method will parse about any Arabic textual datetime description into a Unix timestamp 785 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 786 */ 787 public function strtotime($text, $now = time) 788 { 789 $text = $this->_convert($text, $this->inputCharset, 'utf-8'); 790 791 $int = ArStrToTime::strtotime($text, $now); 792 793 return $int; 794 } 795 796 /** 797 * @return String Standardized version of input Arabic text 798 * @param String $text Arabic text you would like to standardize 799 * @desc This method will standardize Arabic text to follow writing standards (just like magazine rules) 800 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 801 */ 802 public function standard($str) 803 { 804 $str = $this->_convert($str, $this->inputCharset, 'utf-8'); 805 806 $str = $this->ArStandardization->standard($str); 807 808 $str = $this->_convert($str, 'utf-8', $this->outputCharset); 809 810 return $str; 811 } 812 813 /** 814 * @return Binary The compressed string in binary format 815 * @param String $str The text to compress 816 * @desc Compress the given string using the Huffman-like coding 817 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 818 */ 819 public function compress($string) 820 { 821 $string = $this->_convert($string, $this->inputCharset, 'windows-1256'); 822 823 $bin = ArCompressStr::compress($string); 824 825 return $bin; 826 } 827 828 /** 829 * @return String The original uncompressed string 830 * @param Binary $bin The text compressed by compress(). 831 * @desc Uncompress a compressed string 832 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 833 */ 834 public function decompress($binary) 835 { 836 $str = ArCompressStr::decompress($binary); 837 838 $str = $this->_convert($str, 'windows-1256', $this->outputCharset); 839 840 return $str; 841 } 842 843 /** 844 * @return Boolean True if found and False if not found 845 * @param Binary $bin Compressed binary string 846 * @param String $word The string you looking for 847 * @desc Search a compressed string for a given word 848 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 849 */ 850 public function search($binary, $word) 851 { 852 $word = $this->_convert($word, $this->inputCharset, 'windows-1256'); 853 854 $boolean = ArCompressStr::search($binary, $word); 855 856 return $boolean; 857 } 858 } 2 /** 3 * ---------------------------------------------------------------------- 4 * 5 * Copyright (C) 2009 by Khaled Al-Shamaa. 6 * 7 * http://www.ar-php.org 8 * 9 * ---------------------------------------------------------------------- 10 * 11 * LICENSE 12 * 13 * This program is open source product; you can redistribute it and/or 14 * modify it under the terms of the GNU Lesser General Public License (LGPL) 15 * as published by the Free Software Foundation; either version 3 16 * of the License, or (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU Lesser General Public License for more details. 22 * 23 * You should have received a copy of the GNU Lesser General Public License 24 * along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>. 25 * 26 * ---------------------------------------------------------------------- 27 * 28 * Class Name: PHP and Arabic Language 29 * 30 * Filename: Arabic.php 31 * 32 * Original Author(s): Khaled Al-Sham'aa <khaled.alshamaa@gmail.com> 33 * 34 * Purpose: Set of PHP classes developed to enhance Arabic web 35 * applications by providing set of tools includes stem-based searching, 36 * translitiration, soundex, Hijri calendar, charset detection and 37 * converter, spell numbers, keyboard language, Muslim prayer time, 38 * auto-summarization, and more... 39 * 40 * ---------------------------------------------------------------------- 41 * 42 * @desc Set of PHP classes developed to enhance Arabic web 43 * applications by providing set of tools includes stem-based searching, 44 * translitiration, soundex, Hijri calendar, charset detection and 45 * converter, spell numbers, keyboard language, Muslim prayer time, 46 * auto-summarization, and more... 47 * @category Text 48 * @package Arabic 49 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 50 * @copyright 2009 Khaled Al-Shamaa 51 * 52 * @license LGPL <http://www.gnu.org/licenses/lgpl.txt> 53 * @version 2.5.2 released in Sep 16, 2009 54 * @link http://www.ar-php.org 55 */ 56 57 // New in PHP V5.3: Namespaces 58 // namespace Arabic; 59 60 //error_reporting(E_STRICT); 61 $use_exception = false; 62 $use_autoload = false; 63 64 /** 65 * Error handler function 66 * 67 * @param int $errno The level of the error raised 68 * @param string $errstr The error message 69 * @param string $errfile The filename that the error was raised in 70 * @param int $errline The line number the error was raised at 71 * 72 * @return boolean FALSE 73 */ 74 function myErrorHandler($errno, $errstr, $errfile, $errline) 75 { 76 if ($errfile == __FILE__ || file_exists(dirname(__FILE__).'/'.basename($errfile))) { 77 $msg = '<b>Arabic Class Exception:</b> '; 78 $msg .= $errstr; 79 $msg .= " in <b>$errfile</b>"; 80 $msg .= " on line <b>$errline</b><br />"; 81 82 throw new ArabicException($msg, $errno); 83 } 84 85 // If the function returns false then the normal error handler continues 86 return false; 87 } 88 89 if ($use_exception) { 90 set_error_handler('myErrorHandler'); 91 } 92 93 /** 94 * Core PHP and Arabic language class 95 * 96 * @category Text 97 * @package Arabic 98 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 99 * @copyright 2009 Khaled Al-Shamaa 100 * 101 * @license LGPL <http://www.gnu.org/licenses/lgpl.txt> 102 * @link http://www.ar-php.org 103 */ 104 class Arabic 105 { 106 protected $_inputCharset = 'utf-8'; 107 protected $_outputCharset = 'utf-8'; 108 protected $_path; 109 110 public $myObject; 111 public $myClass; 112 113 /** 114 * Load selected library/sub class you would like to use its functionality 115 * 116 * @param string $library [ArAutoSummarize|ArCharsetC|ArCharsetD|ArDate| 117 * ArGender|ArGlyphs|ArIdentifier|ArKeySwap|ArMktime|ArNumbers| 118 * ArQuery|ArSoundex|ArStrToTime|ArTransliteration|ArWordTag| 119 * EnTransliteration|Salat|ArCompressStr|ArStandard|ArStemmer] 120 * 121 * @desc Load selected library/sub class you would like to use its functionality 122 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 123 */ 124 public function __construct($library) 125 { 126 if($library) $this->load($library); 127 } 128 129 public function load($library) 130 { 131 global $use_autoload; 132 133 $this->myClass = $library; 134 135 if (!$use_autoload) { 136 $this->_path = strtr(__FILE__, '\\', '/'); 137 $this->_path = substr($this->_path, 0, strrpos($this->_path, '/')); 138 139 include_once $this->_path.'/'.$this->myClass.'.class.php'; 140 } 141 142 $this->myObject = new $library(); 143 $this->{$library} = &$this->myObject; 144 } 145 146 /** 147 * The magic method __call() allows to capture invocation of non existing methods. 148 * That way __call() can be used to implement user defined method handling that 149 * depends on the name of the actual method being called. 150 * 151 * @param string $methodName Method name 152 * @param array $arguments Array of arguments 153 * 154 * @return The value returned from the __call() method will be returned to 155 * the caller of the method. 156 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 157 */ 158 public function __call($methodName, $arguments) 159 { 160 // Create an instance of the ReflectionMethod class 161 $method = new ReflectionMethod($this->myClass, $methodName); 162 163 $params = array(); 164 $parameters = $method->getParameters(); 165 166 foreach ($parameters as $parameter) { 167 $name = $parameter->getName(); 168 $value = array_shift($arguments); 169 if (is_null($value) && $parameter->isDefaultValueAvailable()) $value = $parameter->getDefaultValue(); 170 if ($name == 'main') $value = $this; 171 $params[$name] = $value; 172 } 173 174 return call_user_func_array(array(&$this->myObject, $methodName), $params); 175 } 176 177 /** 178 * Garbage collection, release child objects directly 179 * 180 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 181 */ 182 public function __destruct() 183 { 184 $this->_inputCharset = null; 185 $this->_outputCharset = null; 186 $this->_path = null; 187 $this->myObject = null; 188 $this->myClass = null; 189 } 190 191 /** 192 * Set charset used in class input Arabic strings 193 * 194 * @param string $charset Input charset [utf-8|windows-1256|iso-8859-6] 195 * 196 * @return TRUE if success, or FALSE if fail 197 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 198 */ 199 public function setInputCharset($charset) 200 { 201 $flag = true; 202 203 $charset = strtolower($charset); 204 205 if (in_array($charset, array('utf-8', 'windows-1256', 'iso-8859-6'))) { 206 $this->_inputCharset = $charset; 207 } else { 208 $flag = false; 209 } 210 211 return $flag; 212 } 213 214 /** 215 * Set charset used in class output Arabic strings 216 * 217 * @param string $charset Output charset [utf-8|windows-1256|iso-8859-6] 218 * 219 * @return boolean TRUE if success, or FALSE if fail 220 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 221 */ 222 public function setOutputCharset($charset) 223 { 224 $flag = true; 225 226 $charset = strtolower($charset); 227 228 if (in_array($charset, array('utf-8', 'windows-1256', 'iso-8859-6'))) { 229 $this->_outputCharset = $charset; 230 } else { 231 $flag = false; 232 } 233 234 return $flag; 235 } 236 237 /** 238 * Get the charset used in the input Arabic strings 239 * 240 * @return string return current setting for class input Arabic charset 241 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 242 */ 243 public function getInputCharset() 244 { 245 return $this->_inputCharset; 246 } 247 248 /** 249 * Get the charset used in the output Arabic strings 250 * 251 * @return string return current setting for class output Arabic charset 252 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 253 */ 254 public function getOutputCharset() 255 { 256 return $this->_outputCharset; 257 } 258 259 /** 260 * Convert Arabic string from one charset to another 261 * 262 * @param string $str Original Arabic string that you wouldliketo convert 263 * @param string $inputCharset Input charset 264 * @param string $outputCharset Output charset 265 * 266 * @return string Converted Arabic string in defined charset 267 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 268 */ 269 public function coreConvert($str, $inputCharset, $outputCharset) 270 { 271 if ($inputCharset != $outputCharset) { 272 if ($inputCharset == 'windows-1256') $inputCharset = 'cp1256'; 273 if ($outputCharset == 'windows-1256') $outputCharset = 'cp1256'; 274 $conv_str = iconv($inputCharset, "$outputCharset//TRANSLIT", $str); 275 276 if($conv_str == '' && $str != '') { 277 include_once($this->_path.'/ArCharsetC.class.php'); 278 $c = ArCharsetC::singleton(); 279 280 if ($inputCharset == 'cp1256') { 281 $conv_str = $c->win2utf($str); 282 } else { 283 $conv_str = $c->utf2win($str); 284 } 285 } 286 } else { 287 $conv_str = $str; 288 } 289 290 return $conv_str; 291 } 292 293 /** 294 * Convert Arabic string from one format to another 295 * 296 * @param string $str Arabic string in the format set by setInputCharset 297 * @param string $inputCharset (optional) Input charset [utf-8|windows-1256|iso-8859-6] 298 * default value is NULL (use set input charset) 299 * @param string $outputCharset (optional) Output charset [utf-8|windows-1256|iso-8859-6] 300 * default value is NULL (use set output charset) 301 * 302 * @return string Arabic string in the format set by method setOutputCharset 303 * @author Khaled Al-Shamaa <khaled.alshamaa@gmail.com> 304 */ 305 public function convert($str, $inputCharset = null, $outputCharset = null) 306 { 307 if ($inputCharset == null) $inputCharset = $this->_inputCharset; 308 if ($outputCharset == null) $outputCharset = $this->_outputCharset; 309 310 $str = $this->coreConvert($str, $inputCharset, $outputCharset); 311 312 return $str; 313 } 314 } 315 316 /** 317 * Arabic Exception class defined by extending the built-in Exception class. 318 * 319 * @category Text 320 * @package Arabic 321 * @author Khaled Al-Shamaa <khaled@ar-php.org> 322 * @copyright 2009 Khaled Al-Shamaa 323 * 324 * @license LGPL <http://www.gnu.org/licenses/lgpl.txt> 325 * @link http://www.ar-php.org 326 */ 327 class ArabicException extends Exception 328 { 329 /** 330 * Make sure everything is assigned properly 331 * 332 * @param string $message Exception message 333 * @param int $code User defined exception code 334 */ 335 public function __construct($message, $code=0) 336 { 337 parent::__construct($message, $code); 338 } 339 } 859 340 ?> -
hijri/trunk/hijri.php
r128988 r205328 4 4 Plugin URI: http://dev.holooli.com/ 5 5 Description: Display Hijri and/or Gregorian dates in your blog. 6 Version: 0. 86 Version: 0.9 7 7 Author: Khaled Al Hourani 8 8 Author URI: http://holooli.com … … 175 175 function getDate($gregorian_date) { 176 176 $hijri_settings = get_option('hijri'); 177 178 177 $date = $gregorian_date; 179 178 180 179 if ($hijri_settings['hijri']) { 181 180 require_once('Arabic.php'); 181 182 182 $Arabic = new Arabic('ArDate'); 183 184 183 $hijri_date = $Arabic->date('l dS F Y', strtotime($gregorian_date)); 185 184 $date = $hijri_date; -
hijri/trunk/readme.txt
r128988 r205328 3 3 Tags: Calendar, Arabic, Hijri, Gregorian, date 4 4 Requires at least: 2.4 5 Tested up to: 2. 86 Stable tag: 0. 85 Tested up to: 2.9 6 Stable tag: 0.9 7 7 8 Display Hijri and/or Gregorian dates in your blog.8 Display Hijri and/or Gregorian dates on your blog. 9 9 10 10 == Description == 11 11 12 Display Hijri and/or Gregorian dates in your blog.12 Display Hijri and/or Gregorian dates on your blog. 13 13 14 14 == Installation ==
Note: See TracChangeset
for help on using the changeset viewer.