"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Text/Transliteration/ASCII.php" (13 Dec 2019, 2397 Bytes) of package /linux/www/papayacms-core-6.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ASCII.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * papaya CMS
    4  *
    5  * @copyright 2000-2018 by papayaCMS project - All rights reserved.
    6  * @link http://www.papaya-cms.com/
    7  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
    8  *
    9  *  You can redistribute and/or modify this script under the terms of the GNU General Public
   10  *  License (GPL) version 2, provided that the copyright and license notes, including these
   11  *  lines, remain unmodified. papaya is distributed in the hope that it will be useful, but
   12  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13  *  FOR A PARTICULAR PURPOSE.
   14  */
   15 namespace Papaya\Text\Transliteration;
   16 
   17 use Papaya\Utility;
   18 
   19 /**
   20  * Transliterate a utf8 string into an ascii string.
   21  *
   22  * @package Papaya-Library
   23  * @subpackage String
   24  */
   25 class ASCII {
   26   /**
   27    * @var
   28    */
   29   private static $_mapping;
   30 
   31   /**
   32    * @var string
   33    */
   34   private $_language = 'generic';
   35 
   36   /**
   37    * Transliterate a utf8 string into an ascii string.
   38    *
   39    *
   40    * @param string $string
   41    * @param string $language
   42    *
   43    * @return string
   44    */
   45   public function transliterate($string, $language = 'generic') {
   46     $this->_language = empty($language) ? 'generic' : $language;
   47     $result = \preg_replace_callback(
   48       '([\\xC2-\\xDF][\\x80-\\xBF]|
   49         \\xE0[\\xA0-\\xBF][\\x80-\\xBF]|[\\xE1-\\xEC][\\x80-\\xBF]{2}|
   50         \\xED[\\x80-\\x9F][\\x80-\\xBF]|[\\xEE-\\xEF][\\x80-\\xBF]{2}|
   51         \\xF0[\\x90-\\xBF][\\x80-\\xBF]{2}|[\\xF1-\\xF3][\\x80-\\xBF]{3}|
   52         \\xF4[\\x80-\\x8F][\\x80-\\xBF]{2})xS',
   53       function($match) {
   54         $codePoint = Utility\Text\UTF8::getCodePoint($match[0]);
   55         return $this->mapping()->get($codePoint, $this->_language);
   56       },
   57       $string
   58     );
   59     return $result;
   60   }
   61 
   62   /**
   63    * Mapping tables subobject, this is saved statically to improve performance and
   64    * memory consumption.
   65    *
   66    * @param ASCII\Mapping $mapping
   67    *
   68    * @return ASCII\Mapping
   69    */
   70   public function mapping(ASCII\Mapping $mapping = NULL) {
   71     if (NULL !== $mapping) {
   72       self::$_mapping = $mapping;
   73     } elseif (NULL === self::$_mapping) {
   74       self::$_mapping = new ASCII\Mapping();
   75     }
   76     return self::$_mapping;
   77   }
   78 
   79   /**
   80    * The mapping tables are stored statically, so an explicit method is needed to reset them.
   81    */
   82   public function resetMapping() {
   83     self::$_mapping = NULL;
   84   }
   85 }