PHP filters

From TRCCompSci - AQA Computer Science
Revision as of 14:09, 20 December 2017 by Admin (talk | contribs)
Jump to: navigation, search

Filters can be used to both sanitize and validate data.

Validating Integer

This code will check if the value is an integer, 10.5 obviously isn't:

<?php
$int = 10.5;

if (filter_var($int, FILTER_VALIDATE_INT)) {
    echo("Integer is valid");
} else {
    echo("Integer is not valid");
}
?>

Filters available are:

  • int
  • boolean
  • float
  • validate_regexp
  • validate_url
  • validate_email
  • validate_ip
  • string
  • stripped
  • encoded
  • special_chars
  • full_special_chars
  • unsafe_raw
  • email
  • url
  • number_int
  • number_float
  • magic_quotes
  • callback