Home > Backend Development > PHP Tutorial > Share three ways to delete html tags in php

Share three ways to delete html tags in php

WBOY
Release: 2016-07-25 08:58:09
Original
1299 people have browsed it
This article introduces three methods of using php to delete html tags. Friends in need can refer to them.

Method 1, directly take out the desired mark. PHP functions used: str_replace, htmlspecialchars, strip_tags.

<?php
//取出br标记
function strip($str)
{
$str=str_replace("<br>","",$str);
//$str=htmlspecialchars($str);
return strip_tags($str);
}
?>
Copy after login

Method 2, there is a strip_tags function in PHP that can easily remove HTML tags. echo strip_tags(“Hello World”); // Remove HTML, XML and PHP tags. Non-standard HTML code can also be correctly removed: echo strip_tags(“">cftea”); //Output cftea

Use strip_tags function to remove HTML tags.

<?php
$str = ‘www<p>dreamdu</p>.com';
echo(htmlspecialchars($str).”<br>”);
echo(strip_tags($str));
?>
Copy after login

Method 3, strtr function converts specific characters in the string. grammar strtr(string,from,to) or strtr(string,array) Parameter Description string1 required. Specifies the string to be converted. from Required (unless using an array). Specifies the character to be changed. to Required (unless using an array). Specifies the character to change to. array Required (unless from and to are used). An array where the keys are the original characters and the values ​​are the target characters.

Example 1:

<?php
echo strtr("Hilla Warld","ia","eo");
?>
Copy after login

Example 2:

<?php
$arr = array("Hello" => "Hi", "world" => "earth");
echo strtr("Hello world",$arr);
?>
Copy after login
Articles you may be interested in: Two methods to remove HTML tags with php php removes redundant HTML tags Sharing an example of php using strip_tags to completely remove all html tags Usage examples of php function strip_tags for filtering html tags (pictures and text) php deletes html tags and the code of html tags in strings Analysis of the difference between strip_tags and htmlspecialchars in removing html tags with php php function to delete html tags in string Remove the code of html tags in the content Extract php code of html tag Code examples of php regular filtering html tags, spaces, newlines, etc. php removes html tags to obtain input plain text document strip_tags php makes HTML tags automatically complete the code of the closing function php code to implement automatic completion of html tags How to use thinkPHP’s Html template tag


source:php.cn
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