HtmlEntities
#region GetOnlyTextFromHtmlCode + RemoveHtmlChars + RemoveTagFromHtmlCode
/// <summary>
/// http://www.codeproject.com/script/Content/ViewAssociatedFile.aspx?rzp=%2FKB%2Fedit%2FZetaHtmlEditControl%2F%2FZetaHtmlEditControl-Source.zip&zep=Control%2FHtmlEditControl.cs&obid=43954&obtid=2&ovid=13
/// </summary>
/// <param name="htmlCode"></param>
/// <returns></returns>
private static string getOnlyTextFromHtmlCode(string htmlCode)
{
//<br>
htmlCode = htmlCode.Replace("\r\n", @" ");
htmlCode = htmlCode.Replace("\r", @" ");
htmlCode = htmlCode.Replace("\n", @" "); htmlCode = htmlCode.Replace(@"</p>", Environment.NewLine + Environment.NewLine);
htmlCode = htmlCode.Replace(@"</P>", Environment.NewLine + Environment.NewLine); //html comment
htmlCode = Regex.Replace(
htmlCode,
@"<!--.*?-->",
string.Empty,
RegexOptions.Singleline | RegexOptions.IgnoreCase); //<p>
htmlCode = Regex.Replace(htmlCode,
@"<br[^>]*>",
Environment.NewLine,
RegexOptions.Singleline | RegexOptions.IgnoreCase); //tags
htmlCode = removeTagFromHtmlCode(@"style", htmlCode);
htmlCode = removeTagFromHtmlCode(@"script", htmlCode); //html
htmlCode = Regex.Replace(
htmlCode,
"<(.|\n)+?>",
string.Empty,
RegexOptions.Singleline | RegexOptions.IgnoreCase); //umlaute
htmlCode = unescapeHtmlEntities(htmlCode); //whitespaces
htmlCode = Regex.Replace(
htmlCode,
@" +",
@" ",
RegexOptions.Singleline | RegexOptions.IgnoreCase); return htmlCode;
}
/// <summary>
/// http://dev.w3.org/html5/html-author/charref
/// </summary>
/// <param name="htmlCode"></param>
/// <returns></returns>
private static string unescapeHtmlEntities(string htmlCode)
{
htmlCode = htmlCode.Replace(@" ", @" ");
htmlCode = htmlCode.Replace(@"Ä", @"ä");
htmlCode = htmlCode.Replace(@"&absp;", @"");
htmlCode = htmlCode.Replace(@"&obsp;", @"");
htmlCode = htmlCode.Replace(@"&Obsp;", @"");
htmlCode = htmlCode.Replace(@"&ubsp;", @"");
htmlCode = htmlCode.Replace(@"&Ubsp;", @"");
htmlCode = htmlCode.Replace(@"ß", @"ß");
htmlCode = htmlCode.Replace(@"£", @"£");
htmlCode = htmlCode.Replace(@"§", @"§");
htmlCode = htmlCode.Replace(@"©", @"©");
htmlCode = htmlCode.Replace(@"®", @"®");
htmlCode = htmlCode.Replace(@"µ", @"µ");
htmlCode = htmlCode.Replace(@"¶", @"¶");
htmlCode = htmlCode.Replace(@"Ø", @"Ø");
htmlCode = htmlCode.Replace(@"ø", @"Ø");
htmlCode = htmlCode.Replace(@"÷", @"÷");
htmlCode = htmlCode.Replace(@"×", @"×");
return htmlCode;
} private static string removeTagFromHtmlCode(
string tag,
string htmlCode)
{
return Regex.Replace(
htmlCode,
string.Format(@"<{0}.*?</{1}>", tag, tag),
string.Empty,
RegexOptions.Singleline | RegexOptions.IgnoreCase);
}
#endregion
HtmlEntities的更多相关文章
- php htmlentities函数的问题
看到在细说php第二版教程中的函数htmlentities 例子,实际实验没有效果 $str = "一个 'quote' 是<b>bold</b>";ech ...
- PHP 5.4 已废弃 magic_quotes_gpc,PHP安全转义函数详解(addslashes 、htmlspecialchars、htmlentities、mysql_real_escape_string、strip_tags)
1. addslashes() addslashes()对SQL语句中的特殊字符进行转义操作,包括(‘), (“), (), (NUL)四个字符,此函数在DBMS没有自己的转义函数时候使用,但是如果D ...
- htmlentities,html_entity_decode,addslashes
PHP htmlspecialchars_decode() 函数 PHP htmlspecialchars() 函数 PHP html_entity_decode() 函数 PHP中混淆的三组函数总结 ...
- strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes学习小结
一.strip_tags 从字符串中去除 HTML 和 PHP 标记 string strip_tags ( string $str [, string $allowable_tags ] ) str ...
- 关于htmlentities 、htmlspecialchars、addslashes的使用
1.html_entity_decode():把html实体转换为字符. Eg:$str = "just atest & 'learn to use '"; echo ht ...
- [PHP]htmlentities() 函数
定义和用法 htmlentities() 函数把字符转换为 HTML 实体. 语法 htmlentities(string,quotestyle,character-set) 参数 描述 string ...
- htmlentities() 函数
Definition and Usage定义和用法 The htmlentities() function converts characters to HTML entities.htmlentit ...
- strip_tags、htmlentities、htmlspecialchars的区别
一.strip_tags() 函数剥去字符串中的 HTML.XML 以及 PHP 的标签. strip_tags(string,allow) 注释:可通过allow设置允许的标签.这些标签不会被删除. ...
- htmlentities、addslashes 、htmlspecialchars的使用
1.html_entity_decode():把html实体转换为字符. Eg:$str = "just atest & 'learn to use '";echo htm ...
- php过滤字段htmlentities,htmlspecialchars,strip_tags
1.strip_tags:过滤html标签比如<a> <html> <script> 如: $str = '<a href="test.html&q ...
随机推荐
- ansible api2.0 多进程执行不同的playbook
自动化运维工具:ansible 多进程调用ansible api的应用场景: 应用系统检查 一个应用系统可能具有20—50台服务器的集群,初步的系统层面检查可以用一个统一的playbook来检查, ...
- 2016级算法期末上机-E.中等·ModricWang's Fight with DDLs II
1125 ModricWang's Fight with DDLs II 思路 圆内被划分部分数的计算方式如下: 圆内部的每一个交点都使得总份数增加了一:除此之外,每一根直线段最后抵达圆周时,总份数也 ...
- 常见 CentOS 7 安装问题
问题一: 解决办法:网上安装文章推荐的的刻录工具多是 ultraiso,有时候可能会出现错误,这时候最简单的解决办法是换由RedHat推荐的刻录工具,fedora media writer,不过这个工 ...
- Java中静态变量的声明位置
Java中静态变量只能是成员变量,局部方法中的局部变量除final外不能有任何其他修饰符,例如: public class Test { static String x = "1" ...
- 等价类计数问题(Polya定理和burnside引理)
零.约定: (置换等名词会在前置知识中有解释) \(1.\)在本文中,题目要求的染色方案等统称为"元素". \(2.\)两个元素严格相等我们记做"\(=\)", ...
- 关于README的内容
如何在开发项目的时候写出标准的README文件? 为什么要写这篇博客? 关于README的编写,这个也十分的重要,如果你觉得只是随便写写,那么你就错了:编写这部分也是相当的重要的.工欲善其事,必先利其 ...
- Mac下Homebrew安装的软件放在什么地方
一般情况是这么操作的: 1.通过brew install安装应用最先是放在/usr/local/Cellar/目录下. 2.有些应用会自动创建软链接放在/usr/bin或者/usr/sbin,同时也会 ...
- java翻译到mono C#实现系列(3) 获取手机设备信息(残缺,)
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; ...
- 使用大白菜安装Windows Server 2012 r2
依照往常安装win10的习惯操作,结果发现无GUI界面.重装时注意到了两个问题: 1. 启动时有两个U盘启动选项,请选择无UEFI的模式启动: 2. 一键安装系统时,一定要点一下系统文件来源的地方,因 ...
- Sublime 必知必会(持续更新)
1.格式化代码 Edit - Line - Reindent(中文路径则是:编辑 - 行 - 再次缩进) 2.分屏显示 view-layout-Columns:2(中文路径则是:查看 - 布局 - 列 ...