#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(@"&nbsp;", @" ");

      htmlCode = htmlCode.Replace(@"&Auml;", @"ä");
      htmlCode = htmlCode.Replace(@"&absp;", @"");
      htmlCode = htmlCode.Replace(@"&obsp;", @"");
      htmlCode = htmlCode.Replace(@"&Obsp;", @"");
      htmlCode = htmlCode.Replace(@"&ubsp;", @"");
      htmlCode = htmlCode.Replace(@"&Ubsp;", @"");
      htmlCode = htmlCode.Replace(@"&szlig;", @"ß");

      htmlCode = htmlCode.Replace(@"&pound;", @"£");
      htmlCode = htmlCode.Replace(@"&sect;", @"§");
      htmlCode = htmlCode.Replace(@"&copy;", @"©");
      htmlCode = htmlCode.Replace(@"&reg;", @"®");
      htmlCode = htmlCode.Replace(@"&micro;", @"µ");
      htmlCode = htmlCode.Replace(@"&para;", @"¶");
      htmlCode = htmlCode.Replace(@"&Oslash;", @"Ø");
      htmlCode = htmlCode.Replace(@"&oslash;", @"Ø");
      htmlCode = htmlCode.Replace(@"&divide;", @"÷");
      htmlCode = htmlCode.Replace(@"&times;", @"×");

            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的更多相关文章

  1. php htmlentities函数的问题

    看到在细说php第二版教程中的函数htmlentities 例子,实际实验没有效果 $str = "一个 'quote' 是<b>bold</b>";ech ...

  2. PHP 5.4 已废弃 magic_quotes_gpc,PHP安全转义函数详解(addslashes 、htmlspecialchars、htmlentities、mysql_real_escape_string、strip_tags)

    1. addslashes() addslashes()对SQL语句中的特殊字符进行转义操作,包括(‘), (“), (), (NUL)四个字符,此函数在DBMS没有自己的转义函数时候使用,但是如果D ...

  3. htmlentities,html_entity_decode,addslashes

    PHP htmlspecialchars_decode() 函数 PHP htmlspecialchars() 函数 PHP html_entity_decode() 函数 PHP中混淆的三组函数总结 ...

  4. strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes学习小结

    一.strip_tags 从字符串中去除 HTML 和 PHP 标记 string strip_tags ( string $str [, string $allowable_tags ] ) str ...

  5. 关于htmlentities 、htmlspecialchars、addslashes的使用

    1.html_entity_decode():把html实体转换为字符. Eg:$str = "just atest & 'learn to use '"; echo ht ...

  6. [PHP]htmlentities() 函数

    定义和用法 htmlentities() 函数把字符转换为 HTML 实体. 语法 htmlentities(string,quotestyle,character-set) 参数 描述 string ...

  7. htmlentities() 函数

    Definition and Usage定义和用法 The htmlentities() function converts characters to HTML entities.htmlentit ...

  8. strip_tags、htmlentities、htmlspecialchars的区别

    一.strip_tags() 函数剥去字符串中的 HTML.XML 以及 PHP 的标签. strip_tags(string,allow) 注释:可通过allow设置允许的标签.这些标签不会被删除. ...

  9. htmlentities、addslashes 、htmlspecialchars的使用

    1.html_entity_decode():把html实体转换为字符. Eg:$str = "just atest & 'learn to use '";echo htm ...

  10. php过滤字段htmlentities,htmlspecialchars,strip_tags

    1.strip_tags:过滤html标签比如<a> <html> <script> 如: $str = '<a href="test.html&q ...

随机推荐

  1. [Linux] 如何在 Linux 中提取随机数

    如何在 Linux 中提取随机数 一.设备文件 /dev/random & /dev/urandom 字符特殊文件 /dev/random 和 /dev/urandom (存在于Linux 1 ...

  2. linux系统解决boot空间不足

    有时候更新Linux系统是会碰到boot空间不足的错误,原因基本上是安装时boot空间设置问题可以通过删除旧的内核来释放boot空间. ubuntu: 1.查看当前使用内核版本号       unam ...

  3. Android脚本打包

    最近项目中需要添加应用渠道,我使用的是友盟统计,对于不同渠道需要编译不同版本,对于开发者说编译一次,手动操作还是可以接受的,但是项目发布版本频率较高,而且渠道很多,这就是一个体力活,而且手动打包还比较 ...

  4. 2016级算法期末上机-D.简单·AlvinZH's Fight with DDLs I

    1117 AlvinZH's Fight with DDLs I 思路 简单题,动态规划. 本题与期末练习赛B题很相似,而且更为简单些.简化问题:在数字序列上取数,不能取相邻的数. DP数组定义,dp ...

  5. shell-005:备份。

    #  这个案例感觉有点绕,如果是本地机器备份完全没必要.下面带颜色的写法值得我们借鉴,所以还是做下笔记吧 #!/bin/bash # 找出//目录下所有txt结尾的文件,且形成一个列表清单的文件 fi ...

  6. 【算法笔记】A1063 Set Similarity

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  7. Codeforces Round #556 (Div. 2)-ABC(这次的题前三题真心水)

    A. Stock Arbitraging 直接上代码: #include<cstdio> #include<cstring> #include<iostream> ...

  8. ospf基础理论

    OSPF简介 OSPF(Open Shortest Path First 开放式最短路径优先)协议是IETF为IP网络开发的IGP路由选择协议.它是一种典型的链路状态(link-state)路由协议. ...

  9. python 进程和线程(代码知识部分)

    二.代码知识部分 一 multiprocessing模块介绍: python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情 ...

  10. (转)mysql 备份与恢复mysqlhotcopy

    原文:http://fuwenchao.blog.51cto.com/6008712/1331910 mysqlhotcopy是一个Perl脚本,最初由Tim Bunce编写并提供.它使用LOCK T ...