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

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

怪我咯
Release: 2023-03-08 06:30:02
Original
1770 people have browsed it

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;                        
?>
Copy after login

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);
?>
Copy after login

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);
?>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template