Filter The Special Characters
<?php $String="asdf-32^3###3sa435r"; $String=preg_replace('/[^a-zA-Z0-9_ -]/s', '', $String); echo $String; /** output **/ asdf-3233sa435r ?>
Validate Integer: FILTER_VALIDATE_INT
<?php $int = "123"; if(!filter_var($int, FILTER_VALIDATE_INT)) { echo($int." Integer is not valid"); } else { echo($int." Integer is valid"); } ?> <!-- OUTPUT 123.1 Integer is not valid 123 Integer is valid -->
Validate Integer With Range: FILTER_VALIDATE_INT
<?php $var=201; $int_options = array( "options"=>array ( "min_range"=>0, "max_range"=>200 ) ); if(!filter_var($var, FILTER_VALIDATE_INT, $int_options)) { echo($var." Integer is not valid"); } else { echo($var." Integer is valid"); } ?> <!-- OUTPUT 200 Integer is valid 201 Integer is not valid -->
Validate Email: FILTER_VALIDATE_EMAIL
<?php $email = "[email protected]"; if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo($email." Email is not valid"); } else { echo($email." Email is valid"); } ?> <!-- OUTPUT mail@gmailcom Email is not valid [email protected] Email is valid -->
Validate Email With _GET Method: FILTER_VALIDATE_EMAIL
<?php if(!filter_has_var(INPUT_GET, "email")) { echo("Input type does not exist"); } else { if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL)) { echo "E-Mail is not valid"; } else { echo "E-Mail is valid"; } } ?>
Validate Integer With _POST Method: FILTER_VALIDATE_INT
<?php $int = "123"; if(!filter_has_var(INPUT_POST, "integer")) { echo("Input type does not exist"); } else{ if(!filter_input(INPUT_POST,'integer', FILTER_VALIDATE_INT)) { echo($int." Integer is not valid"); } else { echo($int." Integer is valid"); } } ?> <!-- OUTPUT 123.1 Integer is not valid 123 Integer is valid -->