PHP remove HTML and PHP tags from string
In this article, you will learn a simple process to remove HTML and PHP tags from a string and file. This is basically used to extract tags from content.
PHP provides predefined functions 'strip_tags()' and 'filter_var()' to remove certain tags from the string.
PHP strip_tags( ) function
This function strips HTML, PHP or specified tags from a string. It also provides functionality to strict certain tags not to remove from the string. In the second parameter, we can pass allowable tags not to remove.
Syntax of strip_tags()
strip_tags($str ,$allow_tags)
Here, $str is the input string and the required parameter and $allow_tags specifies allowed tags and which is an optional parameter. The function returns stripped string after removing HTML, PHP tags and Null bytes.
Example
<?php
$about = "<p>Our mission is to provide good educational resources to "
."technical students, learner, web developers. "
."The etutorialspoint.com is envisioned and developed to meet "
."basic and free courses to learn, succeed and belong.</p>";
$html = '<a href="http://www.oreilly.com">Welcome to <b>etutorialspoint.com. </b></a>';
$html .= '<?php echo $about ?>';
print strip_tags($html);
?>
PHP filter_var()
This function is used to filter a variable with a specified filter. We can use this for both validating and sanitising data.
Syntax of filter_var()
filter_var($var, $filtername, $options)
The $var is the variable name to filter, the $filtername is the predefined ID or filter name to use and the $options specifies one or more options or flags to use. Both $filtername and $options are optional parameters. This method returns the filtered data on success, or FALSE on failure.
Example of filter_var()
<?php
$about = "<p>Our mission is to provide good educational resources to "
."technical students, learner, web developers. "
."The etutorialspoint.com is envisioned and developed to meet "
."basic and free courses to learn, succeed and belong.</p>";
$html = '<a href="https://www.etutorialspoint.com">Welcome to <b>etutorialspoint.com. </b></a>';
$html .= $about;
print filter_var($html, FILTER_SANITIZE_STRING);
?>
The both above examples return the following output-
Welcome to etutorialspoint.com. Our mission is to provide good educational resources to technical students, learner, web developers. The etutorialspoint.com is envisioned and developed to meet basic and free courses to learn, succeed and belong.
PHP remove HTML and PHP tags from a file
In the above examples, we have removed the HTML and PHP tags from a string. But, what if we want to remove HTML and PHP tags from an open file in the simplest way. Here, we have used the inbuilt 'fgetss()' function of PHP to remove HTML and PHP tags from an open file.
Syntax of fgetss()
fgetss($file, $length, $tags)
Here, $file is the file pointer, $length is the length of data to be received and $tags is the allowed tags which should not be stripped.
Example of fgetss()
<?php
$handle = fopen("index.php", "r") or die($php_errormsg);
while (!feof($handle)) {
$buffer = fgetss($handle, 1024);
echo $buffer;
}
fclose($handle);
?>
Related Articles
PHP sanitize input for MySQLPHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Fix: invalid argument supplied for foreach
Locking files with flock()
How to Pass an Array as URL Parameter in PHP
How to generate pdf in PHP using MySQL and MPDF Library
How to Export MySQL Table data as CSV file in PHP
Read CSV file & Import data into MySQL with PHP
Save an emoji in MySQL using PHP
Google reCAPTCHA v2 PHP example
Fetch data from database in PHP and display in PDF
Fibonacci series program in PHP
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP