文本去掉html标签(html去掉文本框外框线)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
今天给各位分享文本去掉html标签的知识,其中也会对html去掉文本框外框线进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
如何去除html标签得到纯文本内容
在网页刚流行起来的时候,提取html中的文本有一个简单的方法,就是将html文本(包含标记)中的所有以“”符号开头到以“”符号之间的内容去掉即可。
但对于现在复杂的网页而言,用这种方法提取出来的文本会有大量的空格、空行、script段落、还有一些html转义字符,效果很差。
下面用正则表达式来提取html中的文本,
代码的实现的思路是:
a、先将html文本中的所有空格、换行符去掉(因为html中的空格和换行是被忽略的)
b、将head标记中的所有内容去掉
c、将script标记中的所有内容去掉
d、将style标记中的所有内容去掉
e、将td换成空格,tr,li,br,p 等标记换成换行符
f、去掉所有以“”符号为头尾的标记去掉。
g、转换,nbps;等转义字符换成相应的符号
h、去掉多余的空格和空行
代码如下:
using System;
using System.Text.RegularExpressions;
namespace Kwanhong.Utilities
{
/// summary
/// HtmlToText 的摘要说明。
/// /summary
public class HtmlToText
{
public string Convert(string source)
{
string result;
//remove line breaks,tabs
result = source.Replace("\r", " ");
result = result.Replace("\n", " ");
result = result.Replace("\t", " ");
//remove the header
result = Regex.Replace(result, "(head).*(/head)", string.Empty, RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"( )*script([^])*", "script", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"(script).*(/script)", string.Empty, RegexOptions.IgnoreCase);
//remove all styles
result = Regex.Replace(result, @"( )*style([^])*", "style", RegexOptions.IgnoreCase); //clearing attributes
result = Regex.Replace(result, "(style).*(/style)", string.Empty, RegexOptions.IgnoreCase);
//insert tabs in spaces of td tags
result = Regex.Replace(result, @"( )*td([^])*", " ", RegexOptions.IgnoreCase);
//insert line breaks in places of br and li tags
result = Regex.Replace(result, @"( )*br( )*", "\r", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"( )*li( )*", "\r", RegexOptions.IgnoreCase);
//insert line paragraphs in places of tr and p tags
result = Regex.Replace(result, @"( )*tr([^])*", "\r\r", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"( )*p([^])*", "\r\r", RegexOptions.IgnoreCase);
//remove anything thats enclosed inside
result = Regex.Replace(result, @"[^]*", string.Empty, RegexOptions.IgnoreCase);
//replace special characters:
result = Regex.Replace(result, @"", "", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @" ", " ", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"", "", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"", "", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"(.{2,6});", string.Empty, RegexOptions.IgnoreCase);
//remove extra line breaks and tabs
result = Regex.Replace(result, @" ( )+", " ");
result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r");
result = Regex.Replace(result, @"(\r\r)+", "\r\n");
return result;
}
}//end class
}//end namespace
如何去掉富文本编辑器的html标签
去掉富文本编辑器的html标签:可以采用正在表达式,如下
description = description.replace(/(\n)/g, "");
description = description.replace(/(\t)/g, "");
description = description.replace(/(\r)/g, "");
description = description.replace(/\/?[^]*/g, "");
description = description.replace(/\s*/g, "");

如何去掉文本框中的html标签
以前在远标学过文本框得到焦点去边框.html 文件代码清单如下:
!doctype htmlhtml lang="zh" head meta charset="utf-8" title文本框得到焦点去边框/title link rel="stylesheet" href="inputOnblur.css" /head body input type="text" id="input1" script src="inputOnblur.js"/script /body/htmlinputOnblur.js 文件代码清单如下:
var inputO = document.getElementById("input1");inputO.onblur = function() { inputO.style.border = 0; }
wordpress 去掉文本里的html标签
解决方法如下
方法一: 将wp-includes文件夹下的kses.php中的2句话屏蔽掉即可,如
//add_action('init', 'kses_init'); //add_action('set_current_user', 'kses_init');
上面这种做法是去掉wordpress对所有标签的过滤,但将这些开放给注册用户会存在潜在的隐患,不推荐使用!
方法二: 把自己想要不被过滤的标签添加到“不过滤的白名单中”!
具体操作: 在wp-includes文件夹下的kses.php中搜索 $allowedposttags,这是个不过滤标签组成的数组,里面所列即是不过滤的标签集合,假如你发表文章时带有style、script两组标签,又不想被不过滤,可在$allowedposttags中添加下面语句:
'style' = array(), 'script' = array()
将上面两句加入 $allowedposttags的一维数组(即最外面那层array)中即可!!
添加全局变量$allowedposttags的值,添加你所需要启用的标签。这种方法将只对提交的文章启用标签。
如此修改,每次升级wp以后还需要验证代码的有效性(代码的结构有可能变化),然后需要重新修改。总得来说原则是能使用插件就尽量不修改源代码。
关于文本去掉html标签和html去掉文本框外框线的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
