- <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>
php日语全角半角正则表达式验证
最新推荐文章于 2021-10-08 16:01:06 发布