Example #1
0
function unicode_info($cp)
{
    $proplist = array(IntlChar::PROPERTY_ALPHABETIC);
    $methodList = array('isUAlphabetic', 'isUUppercase', 'isupper', 'isULowercase', 'islower', 'isUWhiteSpace', 'isWhitespace', 'istitle', 'isdigit', 'isalpha', 'isalnum', 'isxdigit', 'ispunct', 'ispunct', 'isgraph', 'isblank', 'isdefined', 'isspace', 'iscntrl', 'isMirrored', 'isIDStart', 'isIDPart', 'getBlockCode', 'charName');
    $ncp = IntlChar::ord($cp);
    printf("Codepoint U+%04x\n", $ncp);
    foreach ($proplist as $prop) {
        printf("  hasBinaryProperty(%s): %s\n", IntlChar::getPropertyName($prop), IntlChar::hasBinaryProperty($cp, $prop) ? "true" : "false");
    }
    foreach ($methodList as $method) {
        echo "  {$method}(): ";
        var_dump(IntlChar::$method($cp));
    }
    echo "  charAge(): ", implode('.', IntlChar::charAge($cp)), "\n";
    echo "\n";
}
 /**
  * 「ああ」「あい」のように組み合わせた2文字のひらがなによる配列を返します。
  * @return string[] 要素の数は500以上。
  */
 protected function generateHiraganaWords() : array
 {
     if ($this->hiraganaWords) {
         return $this->hiraganaWords;
     }
     for ($i = \IntlChar::ord('ぁ'), $l = $i + sqrt(500); $i < $l; $i++) {
         $firsts[] = \IntlChar::chr($i);
     }
     $seconds = $firsts;
     foreach ($firsts as $first) {
         foreach ($seconds as $second) {
             $this->hiraganaWords[] = "{$first}{$second}";
         }
     }
     return $this->hiraganaWords;
 }
 /**
  * スペースを除くASCII印字可能文字を、対応する全角形に置き換えます。
  * @param string $str
  * @return string
  */
 protected function convertToFullwidth(string $str) : string
 {
     return preg_replace_callback('/[!-~]/u', function (array $matches) : string {
         return \IntlChar::chr(\IntlChar::ord($matches[0]) - (\IntlChar::ord('!') - \IntlChar::ord('!')));
     }, $str);
 }
 /**
  * @param Dictionary $dictionary
  * @throws \BadMethodCallException $this->textFileOnly が偽、かつ「画像・音声・動画ファイルを含む場合のファイル形式」をCSVファイルのみで構文解析していた場合。
  * @throws EmptyOutputException 該当の辞書形式に変換可能なお題が一つも存在しなかった。
  * @return string[]
  */
 public function serialize(Dictionary $dictionary) : array
 {
     $directoryName = (new \esperecyan\dictionary_php\validator\FilenameValidator())->convertToValidFilenameWithoutExtensionInArchives($dictionary->getTitle());
     foreach ($dictionary->getWords() as $word) {
         $serialized = $this->type === 'Inteligenceω しりとり' ? $this->serializeWordAsShiritori($word) : $this->serializeWordAsQuiz($word, $directoryName);
         if ($serialized !== '') {
             $words[] = $serialized;
         }
     }
     if (empty($words)) {
         throw new EmptyOutputException(sprintf(_('%sの辞書形式に変換可能なお題が見つかりませんでした。'), $this->type));
     }
     $previousSubstituteCharacter = mb_substitute_character();
     mb_substitute_character(\IntlChar::ord(self::SUBSTITUTE_CHARACTER));
     $bytes = mb_convert_encoding($this->serializeMetadata($dictionary, '%') . implode('', $words), 'Windows-31J', 'UTF-8');
     mb_substitute_character($previousSubstituteCharacter);
     $files = $dictionary->getFiles();
     if (!$files && !$this->textFileOnly && $dictionary->getFilenames()) {
         throw new \BadMethodCallException();
     } elseif ($this->type === 'Inteligenceω クイズ' && $files && !$this->textFileOnly) {
         $archive = $this->generateArchive();
         foreach ($files as $file) {
             $archive->addFile($file, "{$directoryName}/" . $file->getFilename());
         }
         $archive->addFromString("{$directoryName}.txt", $bytes);
         $archivePath = $archive->filename;
         $archive->close();
         return ['bytes' => file_get_contents($archivePath), 'type' => 'application/zip', 'name' => $this->getFilename($dictionary, 'zip')];
     } else {
         return ['bytes' => $bytes, 'type' => 'text/plain; charset=Shift_JIS', 'name' => $this->getFilename($dictionary, 'txt')];
     }
 }
Example #5
0
<?php

var_dump(IntlChar::foldCase("X"));
 /**
  * 入力を妥当な拡張子を除くファイル名に変換します。
  * @param string $filenameWithoutExtension NFC適用済みの拡張子を除くファイル名。
  * @return string 制御文字、および空白文字のみで構成されていた場合、ランダムな文字列生成します。
  */
 public function convertToValidFilenameWithoutExtension(string $filenameWithoutExtension) : string
 {
     /** @var string 制御文字、先頭末尾の空白を取り除いた文字列。 */
     $trimed = preg_replace('/^\\p{Z}+|\\p{C}+|\\p{Z}+$/u', '', $filenameWithoutExtension);
     return $trimed === '' ? (new FilenameValidator())->generateRandomFilename() : preg_replace_callback('/^(CON|PRN|AUX|CLOCK\\$|NUL|(COM|LPT)[1-9])$|["*.\\/:<>?\\\\|]+/i', function (array $matches) : string {
         $breakIterator = \IntlCodePointBreakIterator::createCodePointInstance();
         $breakIterator->setText($matches[0]);
         $fullWidthChars = '';
         foreach ($breakIterator as $index) {
             if ($index > 0) {
                 $fullWidthChars .= \IntlChar::chr($breakIterator->getLastCodePoint() + self::BETWEEN_HALF_AND_FULL);
             }
         }
         return $fullWidthChars;
     }, $trimed);
 }
Example #7
0
<?php

/**
 * http://php.net/manual/en/class.intlchar.php
 */
$is = IntlChar::isupper('H');
var_dump($is);
echo '<hr>';
echo IntlChar::charName('@');
 /**
  * すべての符号位置がShift_JISに存在すれば真を返します。
  * @param string $str
  * @retun bool
  */
 protected function isShiftJISable(string $str) : bool
 {
     $previousSubstituteCharacter = mb_substitute_character();
     mb_substitute_character(\IntlChar::ord(self::SUBSTITUTE_CHARACTER));
     $codePoints = preg_split('//u', str_replace(self::SUBSTITUTE_CHARACTER, '', $str), -1, PREG_SPLIT_NO_EMPTY);
     mb_convert_variables('Windows-31J', 'UTF-8', $codePoints);
     mb_substitute_character($previousSubstituteCharacter);
     return !in_array(mb_convert_encoding(self::SUBSTITUTE_CHARACTER, 'Windows-31J', 'UTF-8'), $codePoints);
 }
<?php

/**
 * Precisa da extensão intl ativada
 * Documentação: http://php.net/manual/en/class.intlchar.php
 */
printf('%x', IntlChar::CODEPOINT_MAX);
echo IntlChar::charName('@');
var_dump(IntlChar::ispunct('!'));
Example #10
0
 private function getExceptionMessage(string $byte = null) : string
 {
     if ($byte === null) {
         return sprintf("Line %d: Unexpected end of file.", $this->getLineNumber());
     }
     $codepoint = $this->scanCodepoint($byte);
     if (\IntlChar::isprint($codepoint)) {
         return sprintf("Line %d: Unexpected '%s'.", $this->getLineNumber(), $codepoint);
     } else {
         return sprintf("Line %d: Unexpected non-printable character \\u{%X}.", $this->getLineNumber(), \IntlChar::ord($codepoint));
     }
 }