Re: [請益] PHP 如何限制 USER 僅能輸入中文

看板PHP作者 (呆呆華)時間17年前 (2009/01/15 09:03), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《cinsgreen (小綠)》之銘言: : 各位板友好 : 想請問 PHP 有辦法判斷 USER 輸入的字串是否為中文呢? : 目前有的想法是將 USER 輸入的字串丟到陣列 : 再一個一個比對 ASCII 碼來比對輸入的字串是否為英文 : 想請教是否有更容易的作法 : 謝謝 常用的判斷格式的方法, 參考看看吧!! function check_data_format($str, $type) { $chk = 0; switch($type) { case "account": //小寫英文,數子及_-.等符號 $chk = preg_match("/^[a-z0-9_\-\.]{6,}$/", $str); break; case "password": //大小寫英文,數子及_-.等符號 $chk = preg_match("/^[\w_\-\.]{6,}$/", $str); break; case "date": //日期 (Ex: 2009/1/1 or 2009-1-1) $chk = preg_match("/^\d{4}[-\/]\d{1,2}[-\/]\d{1,2}$/", $str); break; case "email": //Mail $chk = preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/", $str); break; case "url": //網址 $chk = preg_match("/^https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?$/", $str); break; case "chinese": //中文但不支援標點符號s $chk = preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $str); break; case "eng&num": //英數字 $chk = preg_match("/^\w+$/", $res); break; } if (!$chk) retrun false; else return true; } -- 西之西處,大陸彼岸,我族飛舞,乘馭他風... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 202.39.223.4
文章代碼(AID): #19Rej8FL (PHP)
文章代碼(AID): #19Rej8FL (PHP)