-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_var.php
52 lines (46 loc) · 1.94 KB
/
filter_var.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
//---------------------filter sanitize-------------------------
/*
FILTER_SANITIZE_EMAIL : remove any chars is not valid in email
*/
$email ='test< >@gmail.com';
$filterd_email = filter_var($email,FILTER_SANITIZE_EMAIL);
echo "email before is : $email and after is : $filterd_email <br>";
//output is : convert from test< >@gmail.com to test@gmail.com
/*
FILTER_SANITIZZE_FLOAT : remove any chars exept digits,+- and optionally .,eE
*/
$number_float= filter_var('235.88,7 5+2test number float',FILTER_SANITIZE_NUMBER_FLOAT);
echo $number_float."<br>";
//output : 2358875+2
// to make the fraction allow ( . ) use additional filter FILTER_FLAG_ALLOW_FRACTION
$number_float= filter_var('235.88,7 5+2test number float',FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
echo $number_float."<br>";
//output: 235.8875+2
// to allow ( , ) use FILTER_FLAG_ALLOW_THOUSAND
$number_float= filter_var('235.88,7 5+2test number float',FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_THOUSAND);
echo $number_float."<br>";
//output: 23588,75+2
//to allow use of (e or E) use FILTER_FLAG_ALLOW_SCIENTIFIC
$number_float= filter_var('235.88,7 5+2test numbr float',FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_SCIENTIFIC);
echo $number_float."<br>";
//output: 2358875+2e
/*
*FILTER_SANITIZE_NUMBER_INT : Remove all characters except digits, plus and minus sign.
*/
$numberInt = filter_var('22.5 + 5,8/7 int', FILTER_SANITIZE_NUMBER_INT);
echo $numberInt."<br>";
//output: 225+587
/*
FILTER_SANITIZE_SPECIAL_CHARS : HTML-encode '"<>& and characters with ASCII value less than 32, optionally strip or encode other special characters.
*/
$text = filter_var('<h1> mohamed </h1>',FILTER_SANITIZE_SPECIAL_CHARS);
echo $text ."<br>";
//output : <h1> mohamed </h1>
/*
*FILTER_SANITIZE_STRING : remove html elements tags
*/
$text = filter_var('<h1> mohamed </h1>',FILTER_SANITIZE_STRING);//PHP 8.1.0, use htmlspecialchars() instead
echo $text ."<br>";
//output : <h1> mohamed </h1>
?>