Use the PHP function 'strip_tags' to remove HTML and PHP tags from strings

WBOY
Release: 2023-07-24 09:26:01
Original
1515 people have browsed it

Use the PHP function "strip_tags" to remove HTML and PHP tags from strings

In dynamic web development, data is often obtained from user input or database and displayed on the web page. However, sometimes the data entered by users contains HTML tags or PHP codes. In order to ensure the security and reliability of the web page, we need to remove these tags and codes. PHP provides a convenient function "strip_tags" to implement this function.

The "strip_tags" function is used to remove HTML and PHP tags from strings. The syntax of the function is as follows:

strip_tags ( string $str [, string $allowable_tags ] ) : string
Copy after login

Among them, the parameter $str is the string to be processed, and the parameter $allowable_tags is an optional parameter used to specify the tags that are allowed to be retained. Other tags will be removed. If the $allowable_tags parameter is not specified, all tags will be removed.

An example is given below to demonstrate how to use the "strip_tags" function to remove HTML and PHP tags from a string.

<?php
$str = "<h1>Welcome to my website!</h1>";
echo "原始字符串:".$str."<br>";

$filtered_str = strip_tags($str);
echo "处理后的字符串:".$filtered_str."<br>";
?>
Copy after login

In the above example, we defined a variable $str whose value is a string containing the

tag. Then, we call the "strip_tags" function to remove the HTML tags from the string and assign the result to the variable $filtered_str. Finally, we output the original string and the processed string to the web page through the echo statement.

Execute the above code, you will get the following output:

原始字符串:<h1>Welcome to my website!</h1>
处理后的字符串:Welcome to my website!
Copy after login

You can see that after processing by the "strip_tags" function, the HTML tags in the original string are successfully removed, leaving only Plain Text.

In addition to removing all tags, you can also retain specified tags by specifying the $allowable_tags parameter. An example is given below to demonstrate how to use the "strip_tags" function to retain specified tags:

<?php
$str = "<p><strong>Welcome</strong> to my <a href='https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b'>website</a>!</p>";
echo "原始字符串:".$str."<br>";

$filtered_str = strip_tags($str, "<p><strong>");
echo "处理后的字符串:".$filtered_str."<br>";
?>
Copy after login

In the above example, we used a tag that contains

, and String. By specifying the $allowable_tags parameter as "

" we retain the

and tags and remove the others.

Execute the above code, you will get the following output:

You can see that after processing by the "strip_tags" function, the tag in the original string is removed, and < ;p> and tags are preserved.

In short, by using the PHP function "strip_tags", we can easily remove HTML and PHP tags from strings, thereby enhancing the security and reliability of web pages. Whether you want to remove all tags or keep specified tags, you can use the "strip_tags" function to achieve this. Hope this article helps you!

The above is the detailed content of Use the PHP function 'strip_tags' to remove HTML and PHP tags from strings. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!