In web program development, htmlspecialchars, strip_tags, and addslashes functions are the most common. This article will introduce these three functions respectively.
1.strip_tags() function
strip_tags() function removes HTML, XML and PHP tags from strings.
Example
<?php $name="Hello <b>world!</b>"; $tags=strip_tags($name); echo $tags; ?>
2.htmlspecialchars() function
htmlspecialchars() function converts predefined characters into HTML entity.
Specifically, this function will convert the following characters:
& (and) into &
" (double Quotation marks) is converted to "
< (less than) is converted to <
> (greater than) is converted to >
Example
<?php $str = "This is some <b>bold</b> text."; echo htmlspecialchars($str); ?>
3.htmlentities() function
Maybe you are still regretting that htmlspecialchars can only handle 4 html tags, so don't regret it now, htmlentities convert all characters. It’s not unpowerful
Example
<?php $str = "<? PHP?h????>"; echo htmlentities($str); ?>
4. The functions stripslashes and addslashes are a pair. addslashes uses backslashes to quote strings, and stripslashes is Restore the string referenced by addslashes.
This function is generally a necessary step that needs to be processed before database query. This string has a backslash before certain characters for the purpose of database query statements, etc. These characters are single quotes ('), double quotes ("), backslash (/) and NUL (NULL character).
[Related article recommendations]
php remove string tags strip_tags() function example detailed explanation
php addslashes() function and stripslashes() function example detailed explanation
php The difference between htmlspecialchars() and strip_tags functions
The above is the detailed content of A brief introduction to htmlspecialchars, strip_tags, and addslashes functions in php. For more information, please follow other related articles on the PHP Chinese website!