{"id":231,"date":"2025-04-09T20:47:25","date_gmt":"2025-04-09T11:47:25","guid":{"rendered":"https:\/\/itexplore.org\/jp\/?p=231"},"modified":"2025-04-09T20:47:29","modified_gmt":"2025-04-09T11:47:29","slug":"is-alphanumeric-regex-in-php","status":"publish","type":"post","link":"https:\/\/itexplore.org\/jp\/tips\/is-alphanumeric-regex-in-php\/","title":{"rendered":"PHP\u3067\u6587\u5b57\u5217\u304c\u534a\u89d2\u82f1\u6570\u5b57\u3067\u3042\u308b\u304b\u3092\u6b63\u898f\u8868\u73fe\u3067\u30c1\u30a7\u30c3\u30af\u3059\u308b"},"content":{"rendered":"<p>\u534a\u89d2\u82f1\u6570\u5b57\u3092\u6b63\u898f\u8868\u73fe\u3067\u30c1\u30a7\u30c3\u30af\u3059\u308b\u306b\u306f<code><span style=\"background: linear-gradient(transparent 60%,rgba(255, 253, 107, 0.7) 0);\" class=\"vk_highlighter\"><strong>^[a-zA-Z0-9]+$<\/strong><\/span><\/code>\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u306e\u6b63\u898f\u8868\u73fe\u306f\u300c1\u6587\u5b57\u4ee5\u4e0a\u306e\u534a\u89d2\u82f1\u6570\u5b57\u3067\u69cb\u6210\u3055\u308c\u308b\u6587\u5b57\u5217\u300d\u3068\u3044\u3046\u610f\u5473\u3092\u6301\u3061\u30013DModel \u306a\u3069\u306e\u6587\u5b57\u5217\u306b\u30de\u30c3\u30c1\u3057\u307e\u3059\u3002<\/p><p>\u3055\u3089\u306b\u3001\u56fa\u5b9a\u9577\u306e\u534a\u89d2\u82f1\u6570\u5b57\u3092\u6307\u5b9a\u3057\u305f\u3044\u5834\u5408\u306f<code><span>^[a-zA-Z0-9]{n}$<\/span><\/code>\u3001\u4f8b\u3048\u30706\u6841\u306e\u5834\u5408\u306f<code><span>^[a-zA-Z0-9]{6}$<\/span><\/code>\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u540c\u69d8\u306b\u3001\u6700\u5c0f\u6841\u6570\u3084\u6700\u5927\u6841\u6570\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u67d4\u8edf\u306a\u30d1\u30bf\u30fc\u30f3\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u300112\u6841\u4ee5\u4e0a20\u6841\u4ee5\u4e0b\u306b\u5236\u9650\u3057\u305f\u3044\u5834\u5408\u306f<code><span>^[a-zA-Z0-9]{12,20}$<\/span><\/code>\u304c\u6709\u52b9\u3067\u3059\u3002<\/p><p>\u4ee5\u4e0b\u306b\u3088\u304f\u5229\u7528\u3055\u308c\u308b\u6b63\u898f\u8868\u73fe\u30d1\u30bf\u30fc\u30f3\u3092\u307e\u3068\u3081\u307e\u3057\u305f\u3002\u7528\u9014\u306b\u5fdc\u3058\u3066\u3054\u6d3b\u7528\u304f\u3060\u3055\u3044\u3002<\/p><figure class=\"wp-block-table\"><table><tbody><tr><th>#<\/th><th>\u30de\u30c3\u30c1\u6761\u4ef6<\/th><th>\u6b63\u898f\u8868\u73fe\u30d1\u30bf\u30fc\u30f3<\/th><\/tr><tr><td style=\"text-align: right;\">1<\/td><td>\u3059\u3079\u3066\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]+$<\/td><\/tr><tr><td style=\"text-align: right;\">2<\/td><td>\u56fa\u5b9a\u9577 n\u6841\u306e\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]{n}$<\/td><\/tr><tr><td style=\"text-align: right;\">3<\/td><td>n\u6841\u4ee5\u4e0a\u306e\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]{n,}$<\/td><\/tr><tr><td style=\"text-align: right;\">4<\/td><td>m\u6841\u4ee5\u4e0b\u306e\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]{1,m}$<\/td><\/tr><tr><td style=\"text-align: right;\">5<\/td><td>n\u6841\u4ee5\u4e0am\u6841\u4ee5\u4e0b\u306e\u534a\u89d2\u82f1\u6570\u5b57<\/td><td>^[a-zA-Z0-9]{n,m}$<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">\u6b63\u898f\u8868\u73fe\u306e\u30d1\u30bf\u30fc\u30f3\u4e00\u89a7\u3068\u6761\u4ef6<\/figcaption><\/figure><h2>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2><p>\u6b21\u306b\u3001\u5165\u529b\u3055\u308c\u305f\u6587\u5b57\u5217\u304c\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u304b\u3092\u5224\u5b9a\u3059\u308bPHP\u95a2\u6570\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002\u3053\u306e\u95a2\u6570\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u6761\u4ef6\u306b\u5fdc\u3058\u3066\u691c\u8a3c\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p><ul><li><strong>\u6700\u5c0f\u6841\u6570\u3092\u7701\u7565\u3057\u305f\u5834\u5408\uff1a<\/strong> \u6587\u5b57\u5217\u304c\u6307\u5b9a\u3055\u308c\u305f\u6700\u5927\u6841\u6570\u4ee5\u5185\u3067\u3001\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/li><li><strong>\u6700\u5927\u6841\u6570\u3092\u7701\u7565\u3057\u305f\u5834\u5408\uff1a<\/strong> \u6587\u5b57\u5217\u304c\u6307\u5b9a\u3055\u308c\u305f\u6700\u5c0f\u6841\u6570\u4ee5\u4e0a\u3067\u3001\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/li><li><strong>\u6700\u5c0f\u6841\u6570\u30fb\u6700\u5927\u6841\u6570\u3092\u7701\u7565\u3057\u305f\u5834\u5408\uff1a<\/strong> \u6587\u5b57\u5217\u5168\u4f53\u304c\u3001\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/li><\/ul><div class=\"input-field-container source-code-container\"><pre class=\"wp-block-code\"><code>\/**\n * \u6587\u5b57\u5217\u304c\u534a\u89d2\u82f1\u6570\u5b57\u306e\u307f\u3067\u69cb\u6210\u3055\u308c\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002\n *\n * @param string $str \u5165\u529b\u6587\u5b57\u5217\n * @param ?int $minLength \u6700\u5c0f\u6841\u6570 (null\u306e\u5834\u5408\u306f1\u3068\u3057\u3066\u6271\u3044\u307e\u3059)\n * @param ?int $maxLength \u6700\u5927\u6841\u6570 (null\u306e\u5834\u5408\u306f\u6587\u5b57\u5217\u306e\u9577\u3055\u3092\u6700\u5927\u5024\u3068\u3057\u3066\u6271\u3044\u307e\u3059)\n * @return bool \u5165\u529b\u6587\u5b57\u5217\u304c\u6761\u4ef6\u3092\u6e80\u305f\u3059\u5834\u5408\u306ftrue\u3001\u305d\u308c\u4ee5\u5916\u306ffalse\u3092\u8fd4\u3057\u307e\u3059\u3002\n * @throws InvalidArgumentException \u6700\u5c0f\u6841\u6570 \u304c 1 \u672a\u6e80\u306e\u5834\u5408\u3001\u307e\u305f\u306f \u6700\u5927\u6841\u6570 \u304c \u6700\u5c0f\u6841\u6570 \u672a\u6e80\u306e\u5834\u5408\u306b\u4f8b\u5916\u304c\u767a\u751f\u3057\u307e\u3059\u3002\n *\/\nfunction isAlphanumeric(string $str, ?int $minLength = null, ?int $maxLength = null): bool {\n    \/\/ \u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u8a2d\u5b9a\n    $min = $minLength ?? 1;\n    $max = $maxLength ?? mb_strlen($str);\n\n    \/\/ \u5f15\u6570\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\n    if ($min < 1) {\n        \/\/ \u6700\u5c0f\u6841\u6570\u304c1\u672a\u6e80\u306e\u5834\u5408\u306b\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\n        throw new InvalidArgumentException('\u6700\u5c0f\u6841\u6570\u306f1\u4ee5\u4e0a\u306e\u6574\u6570\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002');\n    }\n\n    if (!is_null($maxLength) && $max < $min) {\n        \/\/ \u6700\u5927\u6841\u6570\u304c\u6700\u5c0f\u6841\u6570\u672a\u6e80\u306e\u5834\u5408\u306b\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\n        throw new InvalidArgumentException('\u6700\u5c0f\u6841\u6570\u306f\u6700\u5927\u6841\u6570\u4ee5\u4e0b\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002');\n    }\n\n    \/\/ \u534a\u89d2\u82f1\u6570\u5b57\u306e\u6b63\u898f\u8868\u73fe\u30d1\u30bf\u30fc\u30f3\u3092\u69cb\u7bc9\n    $pattern = is_null($maxLength)\n        ? sprintf('\/^[a-zA-Z0-9]{%d,}$\/', (int)$min) \/\/ \u6700\u5927\u6841\u6570\u304c\u7121\u5236\u9650\u306e\u5834\u5408\n        : sprintf('\/^[a-zA-Z0-9]{%d,%d}$\/', (int)$min, (int)$max);\n\n    \/\/ \u6b63\u898f\u8868\u73fe\u306b\u3088\u308b\u30c1\u30a7\u30c3\u30af\n    return (bool)preg_match($pattern, $str);\n}\n<\/code><\/pre><a id=\"copy-source-code-PHP-isAlphanumeric\" 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><h3 class=\"wp-block-heading\">\u691c\u8a3c<\/h3><p>\u7bc4\u56f2\u306f\u30d0\u30a4\u30c8\u6570\u3067\u306f\u306a\u304f<span data-color=\"#fffd6b\" style=\"background: linear-gradient(transparent 60%,rgba(255, 253, 107, 0.7) 0);\" class=\"vk_highlighter\">\u6841\u6570<\/span>\uff08\u6587\u5b57\u6570\uff09\u3067\u6307\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\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>isAlphanumeric(\"<\/span><div class=\"input-field-container inline-block\" style=\"width: 16rem;\"><input type=\"text\" id=\"sentence\" value=\"\" maxlength=\"64\" placeholder=\"\u6587\u5b57\u5217\"><a id=\"delete-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><span>\", <\/span><div class=\"input-field-container inline-block\" style=\"width: 8rem;\"><input type=\"text\" id=\"min-length\" value=\"\" maxlength=\"2\" placeholder=\"\u6700\u5c0f\u6841\u6570\"><a id=\"delete-min-length\" class=\"action-button risky-link no-disable-control\" title=\"\u6700\u5c0f\u6841\u6570\u3092\u524a\u9664\u3057\u307e\u3059\u3002\"><i class=\"fa-regular fa-circle-xmark\"><\/i><\/a><\/div><span>, <\/span><div class=\"input-field-container inline-block\" style=\"width: 8rem;\"><input type=\"text\" id=\"max-length\" value=\"\" maxlength=\"2\" placeholder=\"\u6700\u5927\u6841\u6570\"><a id=\"delete-max-length\" class=\"action-button risky-link no-disable-control\" title=\"\u6700\u5927\u6841\u6570\u3092\u524a\u9664\u3057\u307e\u3059\u3002\"><i class=\"fa-regular fa-circle-xmark\"><\/i><\/a><\/div><span>);<\/span><\/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-input-result-value\" 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\u534a\u89d2\u82f1\u6570\u5b57\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><h5 class=\"wp-block-heading\">\u691c\u8a3c\u7d50\u679c<\/h5><div id=\"execute-function-result\" class=\"verify-container\"><\/div><p class=\"border-spacer\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u534a\u89d2\u82f1\u6570\u5b57\u3092\u6b63\u898f\u8868\u73fe\u3067\u30c1\u30a7\u30c3\u30af\u3059\u308b\u306b\u306f<br \/>\n^[a-zA-Z0-9]+$<br \/>\n\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u3053\u306e\u6b63\u898f\u8868\u73fe\u306f\u300c1\u6587\u5b57\u4ee5\u4e0a\u306e\u534a\u89d2\u82f1\u6570\u5b57\u3067\u69cb\u6210\u3055\u308c\u308b\u6587\u5b57\u5217\u300d\u3068\u3044\u3046\u610f\u5473\u3092\u6301\u3061\u30013DModel \u306a\u3069\u306e\u6587\u5b57\u5217\u306b\u30de\u30c3\u30c1\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":41,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"vkexunit_cta_each_option":"","footnotes":""},"categories":[4],"tags":[9,10],"class_list":["post-231","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips","tag-php","tag-10"],"_links":{"self":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts\/231","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=231"}],"version-history":[{"count":2,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts\/231\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/posts\/231\/revisions\/233"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/media\/41"}],"wp:attachment":[{"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/categories?post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itexplore.org\/jp\/wp-json\/wp\/v2\/tags?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}