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
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>"; ?>
In the above example, we defined a variable $str whose value is a string containing the
Execute the above code, you will get the following output:
原始字符串:<h1>Welcome to my website!</h1> 处理后的字符串:Welcome to my website!
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>"; ?>
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!原始字符串:<p><strong>Welcome</strong> to my <a href='https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b'>website</a>!</p>
处理后的字符串:<p><strong>Welcome</strong> to my website!</p>