PHP strip_tags()

王林
Release: 2024-08-29 12:52:40
Original
1103 people have browsed it

The strip_tags() function in PHP is an inbuilt one which strips/removes a string from HTML and PHP tags. This returns a string having a NULL value in bytes and HTML, PHP tags being removed/stripped from the given input string. This function is basically helpful when we display the input of the user to our site. For example, when we create our message forum on the site a user is able to post a title as follows:

THIS SITE IS BAD!

. If the website was to display all the titles of each post then this unwanted message would also get displayed on the site in heading format to all the visitors of the page. Hence by using this strip_tag() functions it would help in eradicating such issues.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

string strip_tags( $str, $allowed_tags )

Parameters Required: There are 2 parameters which the function accepts;

  • $str being the mandatory parameter and which is the main string which needs to be checked.
  • $allowed_tags is the non-mandatory parameter which describes the tags which are allowed and not to be stripped off. Hence these tags will be retained.

Return Value: This strip_tags function gives us the resultant string stripped off from the input string.

Exceptions:

  • This function does not provide validation of HTML.
  • Some of the things that are stripped by default are the HTML comments and PHP tags and these things cannot be changed as they are hard-coded.
  • The self-closing XHTML tags are ignored in versions after PHP 5.3.4 and hence only non-self-closing tags are allowed to be used in $allowed_tags.

Examples of PHP strip_tags() Function

Let us now take some examples to understand the working of PHP strip_tags function.

Example #1

Code:

<?php<br /> // PHP programme to demostrate<br /> // strip_tags function without $allowed_tags parameter<br /> echo strip_tags("Hello <b>Sample Program!</b>");<br> ?>

Output:

PHP strip_tags()

In the above example we are displaying a simple PHP code to illustrate that strip_tags function can be used without specifying the second parameter to specify which all characters are allowed. This means all the characters in the string are allowed and printed as is.

Example #2

Code:

<?php<br /> $str = '<p>To test a paragraph.</p><!-- Starting comments --> <a href="#fragment">Another paragraph goes here</a>';<br> echo strip_tags($str);<br> echo "n";<br> // Here we allow HTML tag <p><br> echo strip_tags($str, '<p>');<br> // In the version till PHP 7.4.0 the above code can be written as:<br> // echo strip_tags($str, ['p', 'a']);<br> ?>

Output:

PHP strip_tags()

In the above example we are first specifying the required HTML code and assigning the same to the input string $str. We then use strip tag function on that string by using only single parameter. Next we showcase the use of second parameter $allow_tags where we specify only the tag

meaning that only

tag should be allowed and rest all tags should be trimmed off. Hence in the output we can see that only

tag information is displayed and tag information is not displayed. Also we can see that in the output the HTML comments are not printed and hence proves that HTML comments are trimmed by this function by default even if we do not specify in the second parameter list.

Warnings:

  • This function cannot be used to prevent certain attacks like XSS. There are other appropriate methods or functions like htmlspecialchars() which can be used for this purpose depending on what kind of output is required.
  • The function strip_tags() just trims off the HTML tags without really validating them. As a result, this may lead to broken tags or removal of more/less data than what is expected. Hence it should be taken care while specifying the second parameter of the function.
  • This function also does not support any modification on the parameters we specify in the $allowed_tags function also including the onmouseover and style attributes that some user may use to post a text to display to other users.
  • One more thing to note while using this function is that the tag names whose characters are greater than a specified length (1023 bytes) within the HTML will be treated as though invalid, irrespective of what we give in the $allowed_tags as parameters.

Example #3

Code:

<?php<br /> $str = '<a title="" href="/index.html"><b>Some Text</b></a><br> Just a sample text to showcase a paragraph coming in HTML body';<br> echo strip_tags_content($str);<br> function strip_tags_content($str) {<br> return preg_replace('@<(w+)b.*?>.*?</1>@si', '', $str);<br> }<br> ?>

Output:

PHP strip_tags()

In the above example we are using strip_tags function to remove the anchor tag along with its contents in its input string. PHP strip_tags will automatically remove both opening and closing HTML tags when they are being stripped.

Example #4

Code:

<?php<br /> $str = '<?= '<?= 1 ?>' ?>2';<br> var_dump(strip_tags($str));<br> ?>

Output:

PHP strip_tags()

This example shows how to strip contents from a nested PHP tag.

Example #5

Code:

<?php // Test.php<br /> $str = '<br>Trial<br/>on<br />NewLine';<br> $d = strip_tags($str, '<br />');<br> var_dump($d); // Displays string(11) "TraialonNewLine" on output<br> ?>

Output:

PHP strip_tags()

In this example, we can see that we are allowing only contents inside
string and this output here will change as we run in different versions of PHP.

Example #6

Code:

<?php<br> function strip_tags_d($a)<br> {<br> return is_array($a) ?<br> array_map('strip_tags_d', $a) :<br> strip_tags($a);<br> }<br> // Example<br> $arr1 = array('<b>Car</b>', '<i>Bat</i>', array('<b>Car</b>', '<i>Bat</i>'));<br> $arr1 = strip_tags_d($arr1);<br> // Output<br> print_r($arr1);<br> ?>

Output:

PHP strip_tags()

In the above example, we are displaying the use of recursive functions for strip_tags function. Hence in the output we can see that the array is printed in loops of 2.

Conclusion

As shown above, we saw how to strip some unwanted HTML and PHP tags from the code by using the strip_tags() function. This function can parse the input string and extract its structure. It usually trims or replaces the given HTML or PHP tags which we pass as a list of input arguments to be removed from the HTML document. This is also used in cases when we only need to trim PHP tags and not HTML and vice versa.

Recommended Article

This is a guide to the PHP strip_tags(). Here we discuss the Introduction to PHP strip_tags() Function and its examples along with Code Implementation. You can also go through our other suggested articles to learn more-

  1. PHP Frameworks
  2. PHP chop()
  3. PHP setlocale()
  4. PHP strtotime

The above is the detailed content of PHP strip_tags(). For more information, please follow other related articles on the PHP Chinese website!

source:php
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!