How do you correctly use php filters for validation?
<p>I have recently discovered PHP Filter functions for sanitation and validation.</p>

<p>I have been wondering if im using them correctly and struggle to find much info about them, especially flags.</p>

<p>for example I have a sanitize function for email addresses.</p>

<pre><code>function sanitize_email($var){
$var = trim($var);
$var = filter_var($var,FILTER_UNSAFE_RAW);
$var = filter_var($var,FILTER_SANITIZE_EMAIL);
$var = filter_var($var,FILTER_VALIDATE_EMAIL);
return $var;
}
</code></pre>

<p>My questions are as follows?</p>

<p>Should i be using a combination of filters? or is FILTER_VALIDATE_EMAIL sufficient to secure the form field on its own?</p>

<p>Ive read that UNSAFE_RAW doesnt work unless you use a flag with it but i cant find a good example showing its use with a flag.</p>

<p>I also have a similar function for sanitizing a text field which looks like this...</p>

<pre><code>function sanitize_text($var){
$var = trim($var);
$var = htmlspecialchars($var);
$var = filter_var($var,FILTER_SANITIZE_STRING);
$var = filter_var($var,FILTER_SANITIZE_MAGIC_QUOTES);
return $var;
}
</code></pre>

<p>Again I'm not sure I really need to do everything that I am with this function. </p>

<p>How do I improve on this function with regard to making the input secure?</p>
