php日语全角半角正则表达式验证
来源:互联网 发布:mac os x lion 下载 编辑:程序博客网 时间:2024/05/17 05:56
- <pre name="code" class="php"><pre name="code" class="php">/**
- * 全角を半角にする
- * @param string $instr 変換前文字列
- * @return string 変換後文字列
- */
- function zen2han($instr) {
- $flag = FALSE; //直前文字が全角カタカナならTRUE
- $len = mb_strlen($instr);
- $ofst = 1;
- $outstr = '';
- for ($ofst = 0; $ofst < $len; $ofst++) {
- $ch = mb_substr($instr, $ofst, 1);
- if ($ch == 'ー') {
- if ($flag) $outstr .= 'ー'; //カタカナの長音記号
- else $outstr .= $ch;
- } else if (mb_ereg_match('[ァ-ヶ]+', $ch)) { //カタカナの範囲
- $outstr .= mb_convert_kana($ch, 'k');
- $flag = TRUE;
- } else if (mb_ereg_match('[0-9]+', $ch)) { //全角数字の範囲
- $outstr .= mb_convert_kana($ch, 'n');
- $flag = TRUE;
- } else if (mb_ereg_match('[A-Z]+', $ch)) { //全角英字の範囲
- $outstr .= mb_convert_kana($ch, 'r');
- $flag = TRUE;
- } else if (mb_ereg_match('[ぁ-ん]+', $ch)) { //ひらがなの範囲
- $flag = TRUE;
- $outstr .= mb_convert_kana($ch, 'h');
- } else {
- $outstr .= $ch;
- $flag = FALSE;
- }
- }
- return $outstr;
- }
- /**
- * 全角のみチェック
- * @param $value
- * [0]: 項目名<br>
- * [1]: value<br>
- * [2]: エラーコード<br> (必要ではない)
- */
- function EC_FULL_ONLY_CHECK($value) {
- if (isset($this->arrErr[$value[0]])) {
- return;
- }
- $this->createParam($value);
- // 値
- $str = $this->arrParam[$value[1]];
- //全角
- for($i = 0; $i < mb_strlen($str, CHAR_CODE); $i++) {
- if ((ord(mb_substr($str, $i, 1, CHAR_CODE)) < 128) || preg_match("/^[ヲ-゚ー]+$/u", $str)) {
- // エラーコードがある場合
- //$this->arrErr[$value[1]] = "※ " . $value[0] . "は全角文字を入力してください。<br />";
- $this->arrErr[$value[1]] = SC_Utils_Ex::sfGetMessage(VALIDATION_STRING_TYPE_ZENKAKU, $value[0]);
- break;
- }
- }
- }
- /**
- * 1半角数字英字文字以上のチェック(EC用)
- *
- * @param $value
- * [0]: 項目名<br>
- * [1]: value<br>
- */
- function EC_HALF_NUMBER_ENG_CHECK($value) {
- if(isset($this->arrErr[$value[1]])) {
- return;
- }
- $this->createParam($value);
- // 値
- $str = $this->arrParam[$value[1]];
- if(preg_match("/^[a-zA-Z]+$/", $str)) {
- //$this->arrErr[$value[1]] = "※ " . $value[0] . "${0}は${1}を含む${2}文字以上を入力してください。<br />";
- $this->arrErr[$value[1]] = SC_Utils_Ex::sfGetMessage (VALIDATION_COMMON_STRING_NOTCONTAIN, array($value[0],'半角英字或るは半角数字',1));
- } else if (preg_match("/^[0-9]+$/", $str)) {
- //$this->arrErr[$value[1]] = "※ " . $value[0] . "${0}は${1}を含む${2}文字以上を入力してください。<br />";
- $this->arrErr[$value[1]] = SC_Utils_Ex::sfGetMessage (VALIDATION_COMMON_STRING_NOTCONTAIN, array($value[0],'半角英字或るは半角数字',1));
- } else if (!preg_match("/^[0-9a-zA-Z]+$/", $str)) {
- //$this->arrErr[$value[1]] = "※ " . $value[0] . "${0}は${1}を含む${2}文字以上を入力してください。<br />";
- $this->arrErr[$value[1]] = SC_Utils_Ex::sfGetMessage (VALIDATION_COMMON_STRING_NOTCONTAIN, array($value[0],'半角英字或るは半角数字',1));
- }
- }
- /**
- * 半角英数字のチェック
- *
- * @param $value
- * [0]: 項目名<br>
- * [1]: value<br>
- */
- function EC_HALF_ENG_AND_NUM_CHECK($value) {
- if(isset($this->arrErr[$value[1]])) {
- return;
- }
- $this->createParam($value);
- if( strlen($this->arrParam[$value[1]]) > 0 && !preg_match("/^[0-9a-zA-Z]+$/", $this->arrParam[$value[1]])) {
- //$this->arrErr[$value[1]] = "※ " . $value[0] . "は半角英数字で入力してください。<br />";
- $this->arrErr[$value[1]] = SC_Utils_Ex::sfGetMessage(VALIDATION_STRING_TYPE_ALNUM, $value[0]);
- }
- }
- /**
- * 半角英数カナのチェック
- *
- * @param $value
- * [0]: 項目名<br>
- * [1]: value<br>
- */
- function EC_HALF_ENG_NUM_KANA_CHECK($value) {
- if(isset($this->arrErr[$value[1]])) {
- return;
- }
- $this->createParam($value);
- if( strlen($this->arrParam[$value[1]]) > 0 && !preg_match("/^[0-9a-zA-Zァ-ヶヲ-゚ー]+$/u", $this->arrParam[$value[1]])) {
- //$this->arrErr[$value[1]] = "※ " . $value[0] . "は半角英数カナ文字を入力してください。<br />";
- $this->arrErr[$value[1]] = SC_Utils_Ex::sfGetMessage(VALIDATION_STRING_TYPE_HANKAKUKANA_ALNUM, $value[0]);
- }
- }
- /**
- * メールアドレス形式の判定
- *
- * @param array $value 各要素は以下の通り。<br>
- * [0]: 項目名<br>
- * [1]: 判定対象を格納している配列キー
- * @return void
- */
- function EC_EMAIL_CHECK( $value ){
- if(isset($this->arrErr[$value[1]])) {
- return;
- }
- $this->createParam($value);
- // 入力がない場合処理しない
- if (strlen($this->arrParam[$value[1]]) === 0) {
- return;
- }
- $wsp = '[\x20\x09]';
- $vchar = '[\x21-\x7e]';
- $quoted_pair = "\\\\(?:$vchar|$wsp)";
- $qtext = '[\x21\x23-\x5b\x5d-\x7e]';
- $qcontent = "(?:$qtext|$quoted_pair)";
- $quoted_string = "\"$qcontent*\"";
- $atext = '[a-zA-Z0-9!#$%&\'*+\-\/\=?^_`{|}~]';
- $dot_atom_text = "$atext+(?:[.]$atext+)*";
- $dot_atom = $dot_atom_text;
- $local_part = "(?:$dot_atom|$quoted_string)";
- $domain = $dot_atom;
- $addr_spec = "${local_part}[@]$domain";
- $dot_atom_loose = "$atext+(?:[.]|$atext)*";
- $local_part_loose = "(?:$dot_atom_loose|$quoted_string)";
- $addr_spec_loose = "${local_part_loose}[@]$domain";
- if (RFC_COMPLIANT_EMAIL_CHECK) {
- $regexp = "/\A${addr_spec}\z/";
- } else {
- // 携帯メールアドレス用に、..や.@を許容する。
- $regexp = "/\A${addr_spec_loose}\z/";
- }
- if (!preg_match($regexp, $this->arrParam[$value[1]])) {
- //$this->arrErr[$value[1]] = "※ " . $value[0] . "の形式が不正です。<br />";
- $this->arrErr[$value[1]] = SC_Utils_Ex::sfGetMessage(VALIDATION_STRING_FORMATINVALID, $value[0]);
- return;
- }
- // 最大文字数制限の判定 (#871)
- $arrValueTemp = $value;
- $arrValueTemp[2] = 256;
- $this->MAX_LENGTH_CHECK($arrValueTemp);
- }
- </pre><br>
- <br>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre>
0 0
- php日语全角半角正则表达式验证
- java 日语全角半角的正则
- 全角/半角的判别by正则表达式
- 正则表达式判断数字,字母的全角或半角
- 校验半角英数字和全角文字的正则表达式
- php 全角转半角
- php 全角转半角
- php 全角数字转半角 与 手机号码验证
- 全角、半角字符长度验证
- php字符串处理之全角半角转换(正则匹配全角字符思路)
- PHP 全角转换半角函数
- php全角转半角函数
- PHP全角标点转为半角
- PHP全角半角转换函数
- php全角转半角函数
- 有关正则表达式判断数字,字符,汉字,全角,半角,英文等
- C#正则表达式——中文/英文空格(全角/半角空格)处理
- C#正则表达式——中文/英文空格(全角/半角空格)处理
- 【django】表单Form类2-自定义校验规则
- JAVA并发编程学习笔记之Unsafe类
- HTTP 1.1与HTTP 1.0的比较及状态码详解
- Springside中使用MyBatis进行分页
- 给创业者的30条建议
- php日语全角半角正则表达式验证
- cron表达式详解
- (四十七)Quartz2D引擎初步
- SQL语句inner join 与left join的区别
- cocos2dx 3.3显示中文
- C语言二级指针与指针数组的结合使用.
- 菜鸟配置hadoop-简单源代码
- Leetcode NO.41 First Missing Positive
- 在辞职后的旅途中:我写了个App 创立了一家公司