Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FuelPHPでひらがな、カタカナ、ひらカタ混在をバリデーションする

Posted at

定義

チェックというよりは、ひらがな・カタカナをチェックした後で全てひらがな、全てカタカナに変換する
ダウンロード(Gist)

fuel/app/classes/validation/japanese.php
<?php

/**
 * 独自バリデーションを追加
 */
class Validation_Japanese
{
	/**
	 * 
	 */
	public static function _validation_hiragana($val)
	{
		if( empty($val) ){ return true; }
		if( static::_validation_hirakata($val) ){
			return mb_convert_kana($val, "sHcV");
		}
		
		return false;
	}
	
	/**
	 * 
	 */
	public static function _validation_katakana($val)
	{
		if( empty($val) ){ return true; }
		if( static::_validation_hirakata($val) ){
			return mb_convert_kana($val, "sKVC");
		}
		
		return false;
	}
	
	/**
	 * 
	 */
	public static function _validation_hirakata($val)
	{
		if( empty($val) ){ return true; }
		
		mb_regex_encoding("UTF-8");
		if (preg_match("/^[  ぁ-んァ-ヶーヲ-゚。-゚0-90-9]+$/u", $val)) {
		    return true;
		}
		
		return false;
	}
}

バリデーション

  • ひらがな
  • カタカナ
  • ひらカタ混在
$val->add_field('name_hiragana', 'name_hiragana', 'required|hiragana|max_length[255]');
$val->add_field('name_katakana', 'name_katakana', 'required|katakana|max_length[255]');
$val->add_field('name_hirakata', 'name_hirakata', 'required|hirakata|max_length[255]');

値確認

echo $val->validated('name_hiragana'); //すべてひらがな
echo $val->validated('name_katakana'); //スベテカタカナ
echo $val->validated('name_hirakata'); //ひらカタこんザイ
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?