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:
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;
Return Value: This strip_tags function gives us the resultant string stripped off from the input string.
Exceptions:
Let us now take some examples to understand the working of PHP strip_tags function.
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:
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.
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:
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:
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:
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.
Code:
<?php<br />
$str = '<?= '<?= 1 ?>' ?>2';<br>
var_dump(strip_tags($str));<br>
?>
Output:
This example shows how to strip contents from a nested PHP tag.
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:
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.
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:
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.
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.
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-
The above is the detailed content of PHP strip_tags(). For more information, please follow other related articles on the PHP Chinese website!