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')]; } }
/** * すべての符号位置が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); }
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)); } }