ホームページ >バックエンド開発 >PHPチュートリアル >データ検証にはPHP独自のフィルタ機能を使用
データ検証にはPHP独自のフィルター関数を使用します
PHP フィルターには 2 つのタイプが含まれます
検証: 検証項目が合法かどうかを検証するために使用されます
サニタイズ: 検証された項目をフォーマットするために使用されるため、検証項目の値を変更し、不正な文字を削除する場合があります 削除など。
PHP公式ドキュメント参照: Filter function collection
参照元: http://www.lai18.com/doc/view/id/410997.html
input_filters_list()
現在のフィルタ関数のリストを表示するために使用されますシステム すべてのフィルターがサポートされています。
<?phpforeach(filter_list() as $id => $filter){ echo $filter.' '.filter_id($filter)."\n";}?>
<?php /*** an integer to check ***/ $int = 1234; /*** validate the integer ***/ echo filter_var($int, FILTER_VALIDATE_INT); //1234?>
<?php /*** an integer to check ***/ $int = 'abc1234'; /*** validate the integer ***/ echo filter_var($int, FILTER_VALIDATE_INT);?>
<?php /*** an integer to check ***/ $int = 42; /*** lower limit of the int ***/ $min = 50; /*** upper limit of the int ***/ $max = 100; /*** validate the integer ***/ echo filter_var($int, FILTER_VALIDATE_INT, array("min_range" => $min, "max_range" => $max)); //42?>
<?php /*** an integer to check ***/ $int = 42; /*** lower limit of the int ***/ $min = 50; /*** upper limit of the int ***/ $max = 100; /*** validate the integer ***/ echo filter_var($int, FILTER_VALIDATE_INT, array("options" => array("min_range" => $min, "max_range" => $max)));?>
<?php /*** an integer to check ***/ $int = 12; /*** lower limit of the int ***/ $min = 10; /*** validate the integer ***/ echo filter_var($int, FILTER_VALIDATE_INT,array('options' => array('min_range' => $min))); //12?>
<?php /*** an array of values to filter ***/ $arr = array(10,"109","", "-1234", "some text", "asdf234asdfgs", array()); /*** create an array of filtered values ***/ $filtered_array = filter_var_array($arr, FILTER_VALIDATE_INT); /*** print out the results ***/ foreach($filtered_array as $key => $value) { echo $key.' -- '.$value.'<br />'; }?>
0 -- 101 -- 1092 -- 3 -- -12344 -- 5 -- 6 -- Array
<?php /*** a hex value to check ***/ $hex = "0xff"; /*** filter with HEX flag ***/ echo filter_var($hex, FILTER_VALIDATE_INT, array("flags" => FILTER_FLAG_ALLOW_HEX)); //255?>
<?php /*** test for a boolean value ***/ echo filter_var("true", FILTER_VALIDATE_BOOLEAN); //1?>
array(6) { [0] => bool(false) [1] => bool(true) [2] => bool(false) [3] => bool(false) [4] => bool(false) [5] => array(5) { [0] => bool(false) [1] => bool(true) [2] => bool(false) [3] => bool(false) [4] => bool(false) }}
<?php /*** an FLOAT value to check ***/ $float = 22.42; /*** validate with the FLOAT flag ***/ if(filter_var($float, FILTER_VALIDATE_FLOAT) === false) { echo "$float is not valid!"; } else { echo "$float is a valid floating point number"; }?>
<?php /*** an array of values ***/ $array = array(1.2,"1.7","", "-12345.678", "some text", "abcd4.2efgh", array()); /*** validate the array ***/ $validation_array = filter_var($array, FILTER_VALIDATE_FLOAT, FILTER_REQUIRE_ARRAY); /*** dump the array of validated data ***/ var_dump($validation_array);?>
array(7) { [0] => float(1.2) [1] => float(1.7) [2] => bool(false) [3] => float(-23234.123) [4] => bool(false) [5] => bool(false) [6] => array(0) { }}
<?php /*** an array of floats with seperators ***/ $floats = array( "1,234" => ",", "1.234" => "..", "1.2e3" => "," ); /*** validate the floats against the user defined decimal seperators ***/ foreach ($floats as $float => $dec_sep) { $out = filter_var($float, FILTER_VALIDATE_FLOAT, array("options" => array("decimal" => $dec_sep))); /*** dump the results ***/ var_dump($out); }?>
float(1.234)Warning: filter_var() [function.filter-var]: decimal separator must be one char in /www/filter.php on line 13bool(false)bool(false)
<?php /*** a rfc compliant web address ***/ $url = "http://www.phpro.org"; /*** try to validate the URL ***/ if(filter_var($url, FILTER_VALIDATE_URL) === FALSE) { /*** if there is no match ***/ echo "Sorry, $url is not valid!"; } else { /*** if we match the pattern ***/ echo "The URL, $url is valid!<br />"; }?>
<?php /*** a non rfc compliant URL ***/ $url = "index.php"; /*** try to validate the URL ***/ if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === FALSE) { /*** if there is no match ***/ echo "Sorry, $url is not valid!"; } else { /*** if the URL is valid ***/ echo "The URL, $url is valid!"; }?>
<?php $email = "someone@exa mple.com"; if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo "E-mail is not valid"; } else { echo "E-mail is valid"; }?>
<?php function convertSpace($string) { return str_replace(" ", "_", $string); } $string = "Peter is a great guy!"; echo filter_var($string, FILTER_CALLBACK,array("options" => "convertSpace"));?>
Peter_is_a_great_guy!