示例#1
0
文件: URLify.php 项目: voku/urlify
 /**
  * Transliterates characters to their ASCII equivalents.
  * $language specifies a priority for a specific language.
  * The latter is useful if languages have different rules for the same character.
  *
  * @param string  $string                            <p>The input string.</p>
  * @param string  $language                          <p>Your primary language.</p>
  * @param boolean $convertToAsciiOnlyViaLanguageMaps <p>
  *                                                   Set to <strong>true</strong> if you only want to convert the
  *                                                   language-maps.
  *                                                   (better performance, but less complete ASCII converting)
  *                                                   </p>
  * @param boolean $convertUtf8Specials               <p>
  *                                                   Convert (html) special chars with portable-utf8 (e.g. \0,
  *                                                   \xE9, %F6, ...).
  *                                                   </p>
  * @param string  $unknown                           <p>Character use if character unknown. (default is ?).</p>
  *
  * @return string
  */
 public static function downcode($string, $language = 'de', $convertToAsciiOnlyViaLanguageMaps = false, $unknown = '', $convertUtf8Specials = true)
 {
     self::init_downcode($language);
     if ($convertUtf8Specials === true) {
         $string = UTF8::urldecode($string);
     }
     $searchArray = array();
     $replaceArray = array();
     if (preg_match_all(self::$regex, $string, $matches)) {
         $matchesCounter = count($matches[0]);
         /** @noinspection ForeachInvariantsInspection */
         for ($i = 0; $i < $matchesCounter; $i++) {
             $char = $matches[0][$i];
             if (isset(self::$map[$char])) {
                 $searchArray[] = $char;
                 $replaceArray[] = self::$map[$char];
             }
         }
     }
     $string = str_replace($searchArray, $replaceArray, $string);
     if ($convertToAsciiOnlyViaLanguageMaps === true) {
         return (string) $string;
     }
     return UTF8::to_ascii($string, $unknown);
 }