#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. 挂载U盘到linux中

        一.  挂载U盘到linux中,也可以是虚拟机中的linux 1. 首先插上U盘 2. fdisk -l 找到自己的U盘设备,并且记住文件系统类型,主要看空间大小来判断,比如是/dev/sdc ...

  2. SQL面试题(网络收集)

    1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name   kecheng   fenshu 张三     语文       81 张三     数学       75 李四     ...

  3. 读取Properties文件的六种方法

    1.使用java.util.Properties类的load()方法 示例: InputStream in = new BufferedInputStream(new FileInputStream( ...

  4. Myeclipse修改项目名称发布后web Context root名称无法修改

    选中项目,右键--->Properties--->在搜索框搜索:deployment as  然后点击显示出的搜索项.修改右侧视图的Web Context Root名称即可. 如图:

  5. mybaits中"#"和"$"的区别

    动态 sql 是 mybatis 的主要特性之一,在 mapper 中定义的参数传到 xml 中之后,在查询之前 mybatis 会对其进行动态解析.mybatis 为我们提供了两种支持动态 sql ...

  6. Mono for Android - LocationServices not working

    Hi,I have the following code in my location activity.(this code was copied from Xamarin's Location S ...

  7. 【ORACLE】ID 2299494.1 安装Oracle 11g 86%报错:Error in invoking target 'agent nmhs' of makefile

    参考: ID 2299494.1 In this Document   Symptoms   Changes   Cause   Solution   References APPLIES TO: O ...

  8. 将静态网页部署到git上访问

    1.将已有的项目放在github上 http://www.cnblogs.com/zqunor/p/6583182.html 2.出现错误解决方案 提交错误 http://blog.csdn.net/ ...

  9. scrapyd远程连接配置

    安装scrapyd: pip install scrapyd 默认scrapyd启动是通过scrapyd就可以直接启动,bind绑定的ip地址是127.0.0.1端口是:6800,这里为了其他主机可以 ...

  10. [PY3]——pwd | grp 模块

    pwd和grp模块都非常简单粗暴,各自分别下面都只有三个函数,来根据/etc/passwd./etc/group文件获取相关信息 getpwuid(UID):根据UID获取用户信息,返回一个list ...