将html格式的内容转换成纯文本格式
2011-01-14 11:38
393 查看
1、[用正则表达式来提取html中的文本]
需要引用的命名空间是System.Text.RegularExpressions
using System.Text.RegularExpressions;
思路:
a、先将html文本中的所有空格、换行符去掉(因为html中的空格和换行是被忽略的)
b、将<head>标记中的所有内容去掉
c、将<script>标记中的所有内容去掉
d、将<style>标记中的所有内容去掉
e、将td换成空格,tr,li,br,p 等标记换成换行符
f、去掉所有以“<>”符号为头尾的标记去掉。
g、转换&,&nbps;等转义字符换成相应的符号
h、去掉多余的空格和空行
方法如下:
/// <summary>
/// 将HTML内容转换成纯文本形式,即去除HTML格式
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string ConvertToText(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");
//remove blank
result = Regex.Replace(result, @"/s", "");
return result;
}
2、[取出文本中的图片地址]
public static string GetImgUrl(string HTMLStr)
{
string str = string.Empty;
string sPattern = @"^<img/s+[^>]*>";
Regex r = new Regex(@"<img/s+[^>]*/s*src/s*=/s*([']?)(?<url>/S+)'?[^>]*>",
RegexOptions.Compiled);
Match m = r.Match(HTMLStr.ToLower());
if (m.Success)
str = m.Result("${url}");
return str;
}
需要引用的命名空间是System.Text.RegularExpressions
using System.Text.RegularExpressions;
思路:
a、先将html文本中的所有空格、换行符去掉(因为html中的空格和换行是被忽略的)
b、将<head>标记中的所有内容去掉
c、将<script>标记中的所有内容去掉
d、将<style>标记中的所有内容去掉
e、将td换成空格,tr,li,br,p 等标记换成换行符
f、去掉所有以“<>”符号为头尾的标记去掉。
g、转换&,&nbps;等转义字符换成相应的符号
h、去掉多余的空格和空行
方法如下:
/// <summary>
/// 将HTML内容转换成纯文本形式,即去除HTML格式
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string ConvertToText(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");
//remove blank
result = Regex.Replace(result, @"/s", "");
return result;
}
2、[取出文本中的图片地址]
public static string GetImgUrl(string HTMLStr)
{
string str = string.Empty;
string sPattern = @"^<img/s+[^>]*>";
Regex r = new Regex(@"<img/s+[^>]*/s*src/s*=/s*([']?)(?<url>/S+)'?[^>]*>",
RegexOptions.Compiled);
Match m = r.Match(HTMLStr.ToLower());
if (m.Success)
str = m.Result("${url}");
return str;
}
相关文章推荐
- Android中将带html格式的数据转换成java识别显示内容
- Android 中如何将带有html格式的文章转换成能在Android中显示的内容
- PHP转换文本框内容为HTML格式的方法
- 把内容转换为html格式存储
- PHP转换文本框内容为HTML格式的方法
- 转换 HTML 与 PDF 格式文档的神器
- 在linux下将html格式转换成xml格式
- HTML学习之按钮内容转换
- 美工作出来的页面都是html格式的~闲来无事~写了个程序用来转换html格式页面为aspx格式,并为之生成cs文件
- 美解决doc、docx格式word转换为Html
- 如何将HTML转换为纯文本格式Text
- 用Excel打开csv文件时,如何处理数字内容展现会自动转换格式的问题
- 微信小程序中显示html格式内容的方法
- iconv内容,convmv文件名,unix2dos,dos2unix文件格式转换,od/cut/wc/dd/diff/uniq/nice/du等命令,linux文件名乱码,文件名,文件内容,vim编码设置
- 正则表达式实现将html文本转换为纯文本格式(将html字符串转换为纯文本方法)
- React 如何解析从后台读取的内容是html格式代码(带样式)
- 用Excel打开csv文件时,如何处理数字内容展现会自动转换格式的问题
- 使用Pandoc命令将Markdown格式文件转换为HTML格式文件
- 文档附件在线预览——HYF数据格式转换软件HTML Filter
- HTML学习之按钮内容转换