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 ?> Copy after login 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); } ?> Copy after login |