#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. pickle 模块学习 常用方法

    内容提要: 1: pickle的主要作用 pickle主要用于python 于python 之间进行文件传出,网络传输 他同json 一样也是有4个函数 pickle.dumps(iterable)  ...

  2. ubuntu下安装ffmpeg

    sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next sudo apt-get update sudo apt-get install ff ...

  3. Python数据类型之数字

    数字(数值) 整数 :123 (int型) 浮点数: 0.25(带小数点的数字即为浮点数,Float型) 布尔值:False,True(即0和1,bool型) 复数 (暂无资料,complex型) 整 ...

  4. request.getSession(true/false)的区别

    javax.servlet.http.HttpServletRequest接口有两个方法:getSession(boolean)和getSession(). 具体什么区别,跟踪源码分析下,先摆出结论: ...

  5. Hexo博客系列(二)-在多台机器上利用Hexo发布博客

    [原文链接]:https://www.tecchen.xyz/blog-hexo-env-02.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力 ...

  6. 防止过拟合:L1/L2正则化

    正则化方法:防止过拟合,提高泛化能力 在训练数据不够多时,或者overtraining时,常常会导致overfitting(过拟合).其直观的表现如下图所示,随着训练过程的进行,模型复杂度增加,在tr ...

  7. 使用mint-ui的 InfiniteScroll 做数据分页请求

    1.首先 npm install mint-ui 2.在main.js引用 import { InfiniteScroll } from 'mint-ui'; Vue.use(InfiniteScro ...

  8. OpenERP中自定义模块卸载失败,Postgres数据库删不掉数据库,OpenERP登录不了一直在加载的问题解决方案。

    解决方案也就是删除掉不用的数据库,OE会提示当前有N个Session不让Drop数据库. 对于Postgres 9.1 版本,在pgAdmin中查询以下语句: SELECT pg_terminate_ ...

  9. Mac下使用tree命令

    Mac下没有tree命令,但是可以通过brew进行安装,命令如下: brew install tree 装好后tree的用法和linux下的保持一致.参考:http://www.cnblogs.com ...

  10. Scope of a Declaration

    6.3. Scope of a Declaration The scope of a declaration of a member m declared in or inherited by an ...