26
26

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.

filter_var, filter_input でよく使うもの

Last updated at Posted at 2017-10-24

基礎

バリデーション
$a= filter_var('1', FILTER_VALIDATE_INT);
$b= filter_var('a', FILTER_VALIDATE_INT);
var_dump($a); //true
var_dump($b); //false

//isset($_POST['input_name'])===falseの場合
$c=filter_input(INPUT_POST, 'input_name', FILTER_SANITIZE_STRING);
var_dump($c); //NULL
除去
$var = filter_var('10.5', FILTER_SANITIZE_NUMBER_INT);
var_dump($var); //105
フィルターのみかける
filter_var($str, FILTER_SANITIZE_STRING);

filter_input(INPUT_POST, 'input_name', FILTER_SANITIZE_STRING);

filter_input_array(INPUT_POST, array(
    'input_name'=>FILTER_SANITIZE_STRING
));
第4引数のフラグは、論理和で複数指定ができる
filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_BACKTICK);

filter_input(INPUT_POST, 'input_name', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_BACKTICK);

filter_input_array(INPUT_POST, array(
    'input_name'=>array(
        'filter' => FILTER_SANITIZE_STRING,
        'flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_BACKTICK
    )));
オプションも指定するとき
$param=array(
    'flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_BACKTICK
    'options' => array('hoge'=>'piyo')
);

filter_var($str, FILTER_SANITIZE_STRING, $param);

filter_input(INPUT_POST, 'input_name', FILTER_SANITIZE_STRING, $param);

#除去フィルター

文字列

HTMLタグを入力させない文字列向け

```php:HTMLタグ除去、ASCIIコード32未満の制御コード除去、[`]除去
filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_BACKTICK);

```php:HTMLタグ除去、ASCIIコード32未満の制御コード除去、[`]除去、マルチバイト文字列除去
filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_BACKTICK | FILTER_FLAG_STRIP_HIGH);

HTMLを想定しない文字列を出力したり格納したりする時

HTMLエスケープ
//htmlspechalchars($str, ENT_QUOTES)と同等
filter_var($str, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

Email

Email
//E-mailに使用できる文字以外削除
filter_var($email, FILTER_SANITIZE_EMAIL);

URL

URLまわり
//URLに使用できる文字以外削除
filter_var($url, FILTER_SANITIZE_URL);

//URLエンコードする時
filter_var($url, FILTER_SANITIZE_ENCODED);

数字

数値まわり
//[123456789+-]以外を除去。小数点が除去され 1.0 は 10 になるので注意
filter_var($num, FILTER_SANITIZE_NUMBER_INT);

//[123456789+-.]以外を除去。小数が使える
filter_var($num, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);

//[0123456789+-.,eE]以外を除去
filter_var($num, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_SCIENTIFIC);

バリデーションフィルター

数値:バリデーションが通らなければデフォルト値0を返す
$num=filter_input(INPUT_POST, 'input_name', FILTER_VALIDATE_INT, array(
        'options'=>array(
            'default' => 0
        )));
数値の範囲を指定
$num=filter_var($maybeNum, FILTER_VALIDATE_INT, array(
        'options'=>array(
            'default' => 1,
            'min_range' => 1,
            'max_range' => 9
        )));
boolean
//"1"、"true"、"on"、"yes"以外をFALSE
$boolean=filter_var($maybeBool, FILTER_VALIDATE_BOOLEAN);
正規表現
$validated=filter_var($var, FILTER_VALIDATE_REGEXP, array('options'=>array('regexp'=>'/^[abcdef]$/')));

参考リンク:PHP正規表現での末尾改行
https://qiita.com/MTattin/items/0ad9af5bbefe15c59c08

26
26
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
26
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?