{"id":351,"date":"2025-05-02T08:23:18","date_gmt":"2025-05-01T23:23:18","guid":{"rendered":"https:\/\/itexplore.org\/jp\/?p=351"},"modified":"2025-05-02T08:23:23","modified_gmt":"2025-05-01T23:23:23","slug":"is-single-byte-string-in-php","status":"publish","type":"post","link":"https:\/\/itexplore.org\/jp\/tips\/is-single-byte-string-in-php\/","title":{"rendered":"PHP\u3067\u6587\u5b57\u5217\u304c\u30b7\u30f3\u30b0\u30eb\u30d0\u30a4\u30c8\u3067\u3042\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b"},"content":{"rendered":"<p>PHP \u3067\u6587\u5b57\u5217\u304c\u30b7\u30f3\u30b0\u30eb\u30d0\u30a4\u30c8\u6587\u5b57\u3060\u3051\u3067\u69cb\u6210\u3055\u308c\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u306b\u306f\u3001\u6587\u5b57\u5217\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u6307\u5b9a\u3057\u3066 <strong><span data-color=\"#fffd6b\" style=\"background: linear-gradient(transparent 60%,rgba(255, 253, 107, 0.7) 0);\" class=\"vk_highlighter\">\u6587\u5b57\u5217\u306e\u9577\u3055\uff08\u6841\u6570\uff09\u3068\u30d0\u30a4\u30c8\u6570\u304c\u4e00\u81f4\u3059\u308b\u3053\u3068<\/span><\/strong> \u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p><p><\/p><h2 class=\"wp-block-heading\">\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2><div class=\"input-field-container source-code-container\"><pre class=\"wp-block-code\"><code>\/**\n * \u6587\u5b57\u5217\u304c\u30b7\u30f3\u30b0\u30eb\u30d0\u30a4\u30c8\u6587\u5b57\u3060\u3051\u3067\u69cb\u6210\u3055\u308c\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\n * @param string $str \u6587\u5b57\u5217\n * @param string $encodingType \u6587\u5b57\u5217\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\n * @return bool \u6587\u5b57\u5217\u304c\u30b7\u30f3\u30b0\u30eb\u30d0\u30a4\u30c8\u6587\u5b57\u3060\u3051\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306ftrue\u3001\u305d\u308c\u4ee5\u5916\u306ffalse\n *\/\nfunction isSingleByteString(string $str, string $encodingType = null) {\n\tif (!$encodingType) {\n\t\t$encodingType = mb_internal_encoding();\n\t}\n\n\tif (!canConvertToTargetEncoding($str, $encodingType)) {\n\t\treturn false;\n\t}\n\n\treturn strlen(mb_convert_encoding($str, $encodingType)) === mb_strlen($str);\n}\n\n\/**\n * \u6587\u5b57\u5217\u304c\u6307\u5b9a\u3057\u305f\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u5909\u63db\u3067\u304d\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\n * @param string $str \u6587\u5b57\u5217\n * @param string $targetEncoding \u5909\u63db\u3057\u305f\u3044\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\n * @return bool \u6307\u5b9a\u3057\u305f\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u5909\u63db\u3067\u304d\u308b\u5834\u5408\u306ftrue\u3001\u305d\u308c\u4ee5\u5916\u306ffalse\n *\/\nfunction canConvertToTargetEncoding($str, $targetEncoding) {\n\t$encoded = mb_convert_encoding($str, $targetEncoding);\n\t$decoded = mb_convert_encoding($encoded, mb_internal_encoding(), $targetEncoding);\n\n\treturn $str === $decoded;\n}\n<\/code><\/pre><a id=\"copy-source-code-PHP-isSingleByteString\" class=\"action-button enable copy-source-code\" title=\"\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3057\u307e\u3059\u3002\"><i class=\"fa-regular fa-copy\"><\/i><\/a><\/div><p class=\"border-spacer\"><\/p><p>isSingleByteString \u95a2\u6570\u306e\u5f15\u6570\u306b\u6307\u5b9a\u3057\u305f\u6587\u5b57\u5217\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u5909\u63db\u3067\u304d\u306a\u3044\u5834\u5408\u306f false \u3092\u8fd4\u3057\u307e\u3059\u3002<\/p><h2 class=\"wp-block-heading\">\u691c\u8a3c<\/h2><p>\u30c1\u30a7\u30c3\u30af\u3057\u305f\u3044\u6587\u5b57\u5217\u3068\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u6307\u5b9a\u3057\u3066\u95a2\u6570\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u6587\u5b57\u5217\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u7701\u7565\u3059\u308b\u3068 UTF-8 \u3067\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p><div class=\"verify-container source-code-container\"><div class=\"wp-block-buttons is-layout-flex wp-block-buttons-between\"><div><span style=\"font-weight:bold\">\u5f15\u6570\u306e\u5165\u529b<\/span><\/div><\/div><p class=\"border-spacer\" style=\"margin-top: -10px;\"><\/p><div class=\"flex-vertical-align-center-container\"><span class=\"inline-block\">isSingleByteString(<\/span><div class=\"flex-vertical-align-center-container\">\"&nbsp;<div class=\"input-field-container inline-block\" style=\"width: 16rem;\"><input type=\"text\" id=\"input-sentence\" value=\"\" maxlength=\"64\" placeholder=\"\u6587\u5b57\u5217\u3092\u5165\u529b\"><a id=\"clear-sentence\" class=\"action-button risky-link no-disable-control\" title=\"\u6587\u5b57\u5217\u3092\u524a\u9664\u3057\u307e\u3059\u3002\"><i class=\"fa-regular fa-circle-xmark\"><\/i><\/a><\/div>&nbsp;\"<\/div><span class=\"inline-block\">,<\/span><div class=\"flex-vertical-align-center-container\">\"&nbsp;<div class=\"input-field-container inline-block\" style=\"width: 12rem;\"><select id=\"encoding-type\" name=\"encoding-type\" title=\"\u5909\u63db\u3057\u305f\u3044\u6587\u5b57\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u9078\u629e\u3057\u307e\u3059\u3002\"><option value=\"\">\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/option><option value=\"UTF-8\">UTF-8<\/option><option value=\"UTF-16\">UTF-16<\/option><option value=\"UTF-32\">UTF-32<\/option><option value=\"SJIS\">SJIS\uff08\u30b7\u30d5\u30c8JIS\uff09<\/option><option value=\"CP932\">CP932\uff08Microsoft\u30b3\u30fc\u30c9\u30da\u30fc\u30b8932\uff09<\/option><option value=\"EUC-JP\">EUC-JP\uff08\u65e5\u672c\u8a9eEUC\uff09<\/option><option value=\"ISO-2022-JP\">ISO-2022-JP\uff08JIS\u30b3\u30fc\u30c9\uff09<\/option><option value=\"ASCII\">ASCII\uff08ASCII\u30b3\u30fc\u30c9\uff09<\/option><option value=\"ISO-8859-1\">ISO-8859-1\uff08Latin-1\uff09<\/option><option value=\"Windows-1252\">Windows-1252\uff08CP1252\uff09<\/option><option value=\"GB2312\">GB2312\uff08\u4fe1\u606f\u4ea4\u6362\u7528\u6c49\u5b57\u7f16\u7801\u5b57\u7b26\u96c6\u30fb\u57fa\u672c\u96c6\uff09<\/option><option value=\"GBK\">GBK\uff08\u6280\u8853\u898f\u7bc4\u6307\u5c0e\u6027\u6587\u4ef6\uff09<\/option><option value=\"GB18030\">GB18030\uff08\u4fe1\u606f\u6280\u672f \u4e2d\u6587\u7f16\u7801\u5b57\u7b26\u96c6\uff09<\/option><option value=\"BIG-5\">BIG-5\uff08\u5927\u4e94\u78bc\uff09<\/option><option value=\"KOI8-R\">KOI8-R<\/option><option value=\"KOI8-U\">KOI8-U<\/option><\/select><\/div>&nbsp;\");<\/div><\/div><p class=\"border-spacer\" style=\"margin-top: -10px;\"><\/p><div class=\"wp-block-buttons is-layout-flex wp-block-buttons-between\"><div class=\"wp-block-button tooltip-container\"><a id=\"clear-information\" class=\"wp-block-button__link wp-element-button risky-action-button\" title=\"\u5f15\u6570\u5165\u529b\u6b04\u3068\u691c\u8a3c\u7d50\u679c\u3092\u30af\u30ea\u30a2\u3057\u307e\u3059\u3002\"><i class=\"fa-solid fa-eraser\"><\/i>\u30af\u30ea\u30a2<\/a><\/div><div class=\"wp-block-button\"><a id=\"execute-function\" class=\"wp-block-button__link wp-element-button\" title=\"\u6587\u5b57\u5217\u304c\u30b7\u30f3\u30b0\u30eb\u30d0\u30a4\u30c8\u6587\u5b57\u3060\u3051\u3067\u69cb\u6210\u3055\u308c\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002\"><i class=\"fa-solid fa-bolt\"><\/i>\u5b9f\u884c<\/a><\/div><\/div><\/div><p class=\"border-spacer\"><\/p><h3 class=\"wp-block-heading\">\u691c\u8a3c\u7d50\u679c<\/h3><div id=\"execute-function-result\" class=\"verify-container\"><\/div><p class=\"border-spacer\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u3067\u6587\u5b57\u5217\u304c\u30b7\u30f3\u30b0\u30eb\u30d0\u30a4\u30c8\u6587\u5b57\u3060\u3051\u3067\u69cb\u6210\u3055\u308c\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u306b\u306f\u3001\u6587\u5b57\u5217\u306e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u6307\u5b9a\u3057\u3066 \u6587\u5b57\u5217\u306e\u9577\u3055\uff08\u6841\u6570\uff09\u3068\u30d0\u30a4\u30c8\u6570\u304c\u4e00\u81f4\u3059\u308b\u3053\u3068 \u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":352,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"vkexunit_cta_each_option":"","footnotes":""},"categories":[4],"tags":[9],"class_list":["post-351","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips","tag-php"],"_links":{"self":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts\/351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/comments?post=351"}],"version-history":[{"count":1,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":353,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts\/351\/revisions\/353"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/media\/352"}],"wp:attachment":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}