Summary of methods to remove specified HTML tags in PHP_PHP tutorial
WBOYOriginal
2016-07-13 16:57:311177browse
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
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='
function strip($str)
{
$str=str_replace(" ","",$str);
//$str=htmlspecialchars($str);
return strip_tags($str);
}
?>
A custom function/
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