PHP Exercise : PHP strip_tags()

Write a program in PHP to remove all html tags except paragraph and italics tags.

Solution

The function strip_tags() does completely get rid of all HTML elements. If you just want to keep some elements (for example, some limited formatting functionalities with <b> and <i> and <br /> tags), you provide a list of allowed values in the second parameter for strip_tags().

Syntax
strip_tags(string,allow)

Here, the first parameter string specifies the string to check and the second parameter allowspecifies allowable tags. These tags will not be removed. The given example demonstrates how to remove all HTML tags except paragraph and italics tags -

<?php
$input = 'The term <i>Official Ireland</i> is commonly <br />' .
	'used in  <b>the Republic of Ireland</b> to denote<br />' .
	' the media, cultural and religious establishment. '.
	'<script>alert("Nice try!");</script>' .
	'<img src="/spam.jpg" />';
	
echo strip_tags($input, '<b><br><i>');
?>

Output of the above code

The term Official Ireland is commonly 
used in  the Republic of Ireland to denote
the media, cultural and religious establishment. alert("Nice try!");




Related PHP Programs for Practice

PHP - Division table program
PHP - Prime Number Program
PHP - Print numbers 10 to 1 using recursion
PHP - Loop through an associative array
PHP - Differentiate between fgets, fgetss and fgetcsv
PHP - Set session on login
PHP - Convert text to speech
PHP - Copying or moving a file
PHP - Locking a file
PHP - CURL Cookie Jar
PHP - Password Hashing
PHP - Emoji Unicode Characters




Read more articles


General Knowledge



Learn Popular Language