In: Computer Science
What does the following PHP code accomplish?
1. function validateInput ( $data ) {
2. $data = trim ( $data );
3. $data = stripslashes ( $data );
4. $data = htmlspecialchars ($data , ENT_QUOTES | ENT_HTML5 ,'UTF -8 ');
5. return $data ;
6. }
Code Snippet:
function validateInput ( $data )
{
$data = trim (
$data );
$data =
stripslashes ( $data );
$data =
htmlspecialchars ($data , ENT_QUOTES | ENT_HTML5 ,'UTF -8 ');
return $data
;
}
Explanation:
The following function validates
the string input and performs some operations.
Like -
trim
($data); -> It
removes the backspace from the string $data and reassign it to
$data.
stripslashes($data) -> It removes the
slashes from the string $data and reassign it to $data.
htmlspecialchars($data , ENT_QUOTES | ENT_HTML5
,'UTF-8'); -> It convert special
character to their HTML equivalent, example '>' will convert
into <.
Test string
$data = " The\ number 5 >
3.";
$data = trim ( $data );
=> $data = "The\
number 5 > 3."
$data = stripslashes ( $data
);
=> $data = "The
number 5 > 3."
$data = htmlspecialchars ($data
, ENT_QUOTES | ENT_HTML5 ,'UTF -8 ');
=> $data = "The
number 5 < 3."
Final String $data = "The number 5 < 3."