Home  >  Article  >  Backend Development  >  A brief introduction to htmlspecialchars, strip_tags, and addslashes functions in php

A brief introduction to htmlspecialchars, strip_tags, and addslashes functions in php

怪我咯
怪我咯Original
2017-06-08 14:16:091718browse

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 "

  • e5aa3845151e1d1ed32da4f8949856a5 (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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn