Home  >  Article  >  Backend Development  >  Summary of methods to remove specified HTML tags in PHP_PHP tutorial

Summary of methods to remove specified HTML tags in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:311105browse

In PHP, our most commonly used specified HTML tags can be directly replaced by the strip_tags function. You can use it to filter all HTML tags. Let me introduce to you other methods besides this function.

Sometimes we need to save html tag pages into the database, but in some cases we need to get pure data without html tags. At this time, we need to process the data with html tags and remove all html tags. Usually, htmlspecialchars() is used to filter html, but the characters of html are escaped, and the html source code is finally displayed. Using strip_tags(), the html tags can be removed.


PHP's default function removes specified html tags, named strip_tags, which is very useful in certain situations.

strip_tags


strip_tags — Strip HTML and PHP tags from a string


string strip_tags ( string str [, string allowable_tags] )

Disadvantages:


This function can only retain the desired html tags, which is the parameter string allowable_tags.

Other uses of the parameter allowable_tags of this function.

The code is as follows Copy code
 代码如下 复制代码


strip_tags($source, ”); 去掉所以的html标签。
strip_tags($source, ‘

’); 保留字符串中的div、img、em标签。

strip_tags($source, ”); Remove all html tags.

strip_tags($source, ‘
’); Retain the div, img, and em tags in the string.


If you want to remove the specified html tag. Then this function cannot meet the needs.
 代码如下 复制代码

function strip_only_tags($str, $tags, $stripContent = FALSE) {
  $content = '';
 
  if (!is_array($tags)) {
    $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
if (end($tags) == '') {
array_pop($tags);
}
}

foreach($tags as $tag) {
if ($stripContent) {
$content = '(.+|s[^>]*>)|)';
    }
 
    $str = preg_replace('#|s[^>]*>)'.$content.'#is', '', $str);
  }
 
  return $str;
}

So I used this function.

The code is as follows Copy code
function strip_only_tags($str, $tags, $stripContent = FALSE) {

$content = '';

if (!is_array($tags)) {
$tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));

If (end($tags) == '') {

       array_pop($tags);

}
 代码如下 复制代码


$target = strip_only_tags($source, array(‘a’,'em’,'b’));移除$source字符串内的a、em、b标签。

$source='


 ';
 
$target = strip_only_tags($source, array('a','em'));
 
//target results
//
Welcome to lixiphp.This a example from!

} foreach($tags as $tag) { If ($stripContent) { $content = '(.+|s[^>]*>)|)'; } $str = preg_replace('#|s[^>]*>)'.$content.'#is', '', $str ); } Return $str; }
Parameter description $str — refers to a string that needs to be filtered, such as div, p, em, img and other HTML tags. $tags — refers to the specified html tags that you want to remove, such as a, img, p, etc. $stripContent = FALSE — Remove the content within the tag, such as deleting the entire link, etc. The default is False, which means the content within the tag will not be deleted. Instructions for use
The code is as follows Copy code
$target = strip_only_tags($source, array(‘a’,’em’,’b’)); Remove the a, em, and b tags in the $source string. $source=' '; $target = strip_only_tags($source, array('a','em')); //target results //
Welcome to lixiphp.This an example from!

Other methods

The code is as follows
代码如下 复制代码


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

Copy code


//Remove the br tag

function strip($str) { $str=str_replace("
","",$str); //$str=htmlspecialchars($str); return strip_tags($str);

}

?> A custom function /
代码如下 复制代码
**
* 取出html标签
*
* @access public
* @param string str
* @return string
*
*/
function deletehtml($str) {
$str = trim($str); //清除字符串两边的空格
$str = strip_tags($str,"

"); //利用php自带的函数清除html格式。保留P标签
    $str = preg_replace("/t/","",$str); //使用正则表达式匹配需要替换的内容,如:空格,换行,并将替换为空。
    $str = preg_replace("/rn/","",$str);
    $str = preg_replace("/r/","",$str);
    $str = preg_replace("/n/","",$str);
    $str = preg_replace("/ /","",$str);
    $str = preg_replace("/  /","",$str);  //匹配html中的空格
    return trim($str); //返回字符串
}
 

The code is as follows Copy code
** * Take out the html tag * * @access public
* @param string str
* @return string * */ function deletehtml($str) { $str = trim($str); //Clear spaces on both sides of the string $str = strip_tags($str,"

"); //Use the function that comes with php to clear the html format. Keep the P tag $str = preg_replace("/t/","",$str); //Use regular expressions to match the content that needs to be replaced, such as spaces, newlines, and replace them with nothing. $str = preg_replace("/rn/","",$str); $str = preg_replace("/r/","",$str); $str = preg_replace("/n/","",$str); $str = preg_replace("/ /","",$str); $str = preg_replace("/ /","",$str); //Match spaces in html Return trim($str); //Return string } http://www.bkjia.com/PHPjc/631529.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631529.htmlTechArticleIn php, our most commonly used specified HTML tags can be directly replaced by the strip_tags function, which can filter all html tag, let me introduce to you other functions besides this...

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