Home  >  Article  >  Backend Development  >  PHP deletes html tags and the code of html tags in strings

PHP deletes html tags and the code of html tags in strings

WBOY
WBOYOriginal
2016-07-25 08:58:081298browse
This article introduces how to use PHP to delete HTML tags, as well as two examples of codes for HTML tags in strings. Friends in need can refer to it.

1. Example of deleting html tags The PHP string processing function strip_tags is used.

<?php
/**
* 取出html标签
*
* @access public
* @param string str
* @return string
*
*/
function deletehtml($str) {
$str = trim($str); //清除字符串两边的空格
$str = strip_tags($str,"<p>"); //利用php自带的函数清除html格式。保留P标签
$str = preg_replace("/\t/","",$str); //使用正 则 表 达 式匹配需要替换的内容,如:空格,换行,并将替换为空。
$str = preg_replace("/\r\n/","",$str);
$str = preg_replace("/\r/","",$str);
$str = preg_replace("/\n/","",$str);
$str = preg_replace("/ /","",$str);
$str = preg_replace("/ /","",$str); //匹配html中的空格
return trim($str); //返回字符串
} //by bbs.it-home.org
?>

2, function to delete HTML tags in strings Sometimes when the page submits content, you want to disable the html tag. The following function can achieve this function:

<?
function delete_htm($scr)
{
for($i=0;$i<strlen($scr);$i++)
{
if(substr($scr,$i,1)=="<")
{
while(substr($scr,$i,1)!=">")$i++;
$i++;
}
$str=$str.substr($scr,$i,1);
}
return($str);
}
?>
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) Three ways to delete html tags in php 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


Statement:
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