etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

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 MySQL
PHP 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




Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP Create Word Document from HTML
-----------------
How to get data from XML file in PHP
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP code to send email using SMTP
-----------------
Characteristics of a Good Computer Program
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
How to add multiple custom markers on google map
-----------------
How to get current directory, filename and code line number in PHP
-----------------
Fibonacci Series Program in PHP
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
How to generate QR Code in PHP
-----------------
Simple pagination in PHP
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
PHP MYSQL Advanced Search Feature
-----------------
PHP Server Side Form Validation
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
jQuery File upload progress bar with file size validation
-----------------
Simple PHP File Cache
-----------------
Simple File Upload Script in PHP
-----------------
Php file based authentication
-----------------
To check whether a year is a leap year or not in php
-----------------
Calculate distance between two locations using PHP
-----------------
PHP User Authentication by IP Address
-----------------
PHP Secure User Registration with Login/logout
-----------------
Simple way to send SMTP mail using Node.js
-----------------
How to print specific part of a web page in javascript
-----------------
Simple Show Hide Menu Navigation
-----------------
Detect Mobile Devices in PHP
-----------------
Polling system using PHP, Ajax and MySql
-----------------
PHP Sending HTML form data to an Email
-----------------
Google Street View API Example
-----------------
Get Visitor\'s location and TimeZone
-----------------
SQL Injection Prevention Techniques
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
Convert MySQL to JSON using PHP
-----------------
Set and Get Cookies in PHP
-----------------
CSS Simple Menu Navigation Bar
-----------------
PHP Programming Error Types
-----------------
Date Timestamp Formats in PHP
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
How to add google map on your website and display address on click marker
-----------------
Write a python program to print all even numbers between 1 to 100
-----------------
How to display PDF file in web page from Database in PHP
-----------------
PHP Getting Document of Remote Address
-----------------
File Upload Validation in PHP
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial






Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2023. All Rights Reserved.