【php正则过滤html标签、空格、换行符的代码附说明】教程文章相关的互联网学习教程文章

PHP正则表达式过滤html标签属性DEMO

过滤html标签在php中可以有内置的函数了,但它过滤的太干净了,我们就整理了一下些利用正则来过滤指定html标签的例子,具体如下所示。采集的时候有时候需要过滤掉多余的标签属性,比如 img标签过滤掉除了src属性之外的所有属性例如删除titile alt等属性以及一些脚的onclick属性等。例如过滤除了src之外的所有属性: 代码如下:$str= preg_replace(/\s(?!src)[a-zA-Z]+=[\\"]{1}[^\\"]+[\\"]{1}/iu, $str); 上面的实例代码是过滤掉除了...

php正则过滤html标签、空格、换行符的代码附说明

代码如下:$str=preg_replace("/\s+/", " ", $str); //过滤多余回车 $str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格) $str=preg_replace("/<\!--.*?-->/si","",$str); //注释 $str=preg_replace("/<(\!.*?)>/si","",$str); //过滤DOCTYPE $str=preg_replace("/<(\/?html.*?)>/si","",$str); //过滤html标签 $str=preg_replace("/<(\/?head.*?)>/si","",$str); //过滤head标签 $str=preg_replace("/<(\/?meta....

smarty中先strip_tags过滤html标签后truncate截取文章运用

strip_tags() 函数剥去 HTML、XML 以及 PHP 的标签。 代码如下:world!”); ?> smarty中可以使用strip_tags去除html标签,包括在之间的任何内容。 例如: index.php: 代码如下:$smarty = new Smarty; $smarty->assign(‘articleTitle, “Blind Woman Gets <span style=”font-family: &amp;”>New Kidney from Dad she Hasnt Seen in years.”); $smarty->display(‘index.tpl); index.tpl: 代码如下:{$articleTitle} {$articleTit...

php转换html标签小例子

php如何转换html标签,使其能在浏览器中正常显示? 在编程中需要把提交的内容转化成html标签,这样才能在浏览器中正常显示。 比如要把<转化成<,把空格 转换成 等。 其实php已有这样的函数html_entity_decode。 例子:$new = htmlspecialchars("Test"); echo $new; //out <a href='test'>Test</a> echo html_entity_decode($new); //out Test ?>

过滤带有html标签的内容

详细内容可以参考 周小蛇的秘密花园 ---- PHP手写方法之 过滤HTML字符 http://zhousir.net/php-html-filtering-method-handwritten-characters.html function html2text($str){ $str = trim($str); $str = preg_replace("/||/isU","",$str); $alltext = ""; $start = 1; for($i=0;$i31){ $alltext .= $str[$i]; } } } $alltext = str_replace(" "," ",$alltext); $alltext = preg_replace("/&([^;&]*)(;|&)/","",$allte...

php去除html标签获得输入纯文本文档strip_tags

<?php $text = Test paragraph. Other text; echo strip_tags($text); echo "\n";// Allowecho strip_tags($text, ); ?>上例将输出: Test paragraph. Other textTest paragraph.Other text

修复未正常关闭的HTML标签(支持嵌套和就近闭合)的代码【图】

/** * fixHtmlTag * * html标签修复函数,此函数可以修复未正确闭合的 HTML 标签 * * 由于不确定性因素太多,暂时提供两种模式“嵌套闭合模式”和 * “就近闭合模式”,应该够用了。 * * 这两种模式是我为了解释清楚此函数的实现而创造的两个名词, * 只需明白什么意思就行。 * 1,嵌套闭合模式,NEST,为默认的闭合方式。即 "你好" * 这样的 html 代码会被修改为 "你好" * 2,就近闭合模式,CLOSE,这种模式会将形如 "你好为什么没...

php正则过滤html标签、空格、换行符等的代码示例

/** desc:正则过滤html标签、空格、换行符等 link:bbs.it-home.org date:2013/2/23*/$str=preg_replace("/\s+/", " ", $str); //过滤多余回车$str=preg_replace("/$str=preg_replace("//si","",$str); //注释$str=preg_replace("//si","",$str); //过滤DOCTYPE$str=preg_replace("//si","",$str); //过滤html标签$str=preg_replace("//si","",$str); //过滤head标签$str=preg_replace("//si","",$str); //过滤meta标签$str=p...

html标签闭合检测与修复【图】

html标签闭合检测与修复,说的有点大 , 并没有考虑的很完整,没有使用正则表达式, 适用于html文件中只有开始标签没有结束标签, 是有结束标签没有开始标签的情况。标签闭合的位置需要根据需求调整 $str = '"> content content full this is content this is content This is cont This is content...

php删除html标签的三种方法分享

本文介绍下,使用php删除html标签的三种方法,有需要的朋友,参考下吧。方法1,直接取出想要的标记。 用到php函数:str_replace、htmlspecialchars、strip_tags。","",$str); //$str=htmlspecialchars($str); return strip_tags($str); } ?> 方法2,PHP 中有个 strip_tags 函数可以方便地去除 HTML 标签。 echo strip_tags(“Hello World”); // 去除 HTML、XML 以及 PHP 的标签。 对于非标准的 HTML 代码也能正确的去除: echo st...

php删除html标签及字符串中html标签的代码

本文介绍下,php实现删除html标签,以及字符串中html标签的两例代码,有需要的朋友,参考下吧。1,删除html标签的例子 用到了php字符串处理函数strip_tags。"); //利用php自带的函数清除html格式。保留P标签 $str = preg_replace("/\t/","",$str); //使用正 则 表 达 式匹配需要替换的内容,如:空格,换行,并将替换为空。 $str = preg_replace("/\r\n/","",$str); $str = preg_replace("/\r/","",$str); $str = preg_replace("/\...

php去除多余的HTML标签

echo strip_tags($str); 2,只保留一个标签的话,只需要将字符串写到strip_tags的第二个参数中。echo strip_tags($str, ""); 3,要保留与…多个标签,只需要将多个标签用空格分隔后写到strip_tags的第二个参数中。echo strip_tags($str, " ");4,保留所有标签,仅仅转义用addslashes(), stripslashes(), htmlspecialchars(), htmlentities(), nl2br() 等函数.addslashes(), stripslashes() 一般是入数据库和出库的时候使用,以免变...

提取html标签的php代码示例

提取html标签的php代码示例 <?php /** * 函数: tags * 功能: 从文件中提取html标签 * * 入口: * $filename 文件名 * $tag 标签名 * 返回: * 数组,每项为: * tagName String * Text String * Attrs Array * * 示例: * print_r(tags("test1.htm","a")); * print_r("http://localhost/index.htm","img"); * */</p> <p>function tags($filename,$tag) { $buffer = join("",file($filename)); $buffer = eregi_replace("\r\n","",$buffe...

php去掉html标签函数代码

<?phpfunction noHTML($content){ $content = preg_replace("/]*>/i",, $content); $content = preg_replace("/<\/a>/i", , $content); $content = preg_replace("/]*>/i",, $content); $content = preg_replace("/<\/div>/i",, $content); $content = preg_replace("/<font[^>]*>/i",, $content); $content = preg_replace("/<\/font>/i",, $content); $content = preg_replace("/<p[^>]*>/i",, $content); ...

php删除字符串HTML标签的函数示例

//删除字符串中html标签//by bbs.it-home.orgfunction delete_htm($scr) { for($i=0;$i{ if(substr($scr,$i,1)=="{ while(substr($scr,$i,1)!=">")$i++; $i++; } $str=$str.substr($scr,$i,1); } return($str); } ?>PHP如何去除HTML标签 方法1: 直接取出想要取出的标记 //取出br标记 function strip($str){$str=str_replace("<br>","",$str);//$str=htmlspecialchars($str);return strip_tags($str);}?>方法2. PHP 中有个 s...