怎么让HTML显示纯文本

怎么让HTML显示纯文本,第1张

在网页刚流行起来的时候,提取html中的文本有一个简单的方法,就是将html文本(包含标记)中的所有以“<”符号开头到以“>”符号之间的内容去掉即可。

但对于现在复杂的网页而言,用这种方法提取出来的文本会有大量的空格、空行、script段落、还有一些html转义字符,效果很差。

下面用正则表达式来提取html中的文本,

代码的实现的思路是:

a、先将html文本中的所有空格、换行符去掉(因为html中的空格和换行是被忽略的)

b、将<head>标记中的所有内容去掉

c、将<script>标记中的所有内容去掉

d、将<style>标记中的所有内容去掉

e、将td换成空格,tr,li,br,p 等标记换成换行符

f、去掉所有以“<>”符号为头尾的标记去掉。

g、转换&,&nbps等转义字符换成相应的符号

h、去掉多余的空格和空行

代码如下:

using System

using System.Text.RegularExpressions

namespace Kwanhong.Utilities

{

/// <summary>

/// HtmlToText 的摘要说明。

/// </summary>

public class HtmlToText

{

public string Convert(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")

return result

}

}//end class

}//end namespace

实现鼠标悬停显示文字,html中使用title属性就可实现显示文字的效果,这个属性还是比较实用的,你可以参考下

<a href="#" title="这里是显示的文字">hello</a>

当鼠标悬停在 hello上一会就会有文字 "这里是显示的文字" 显示。

<!DOCTYPE html>

<html>

  <head>

    <title>文件示例</title>

    <meta name="name" content="content" charset="utf-8">

  </head>

  <body>

      <input type="file" id="file" />

      <input type="button" onclick="readText()" value="File Button">

      <div id="tt">

  

      </div>

  </body>

</html>

<script charset="utf-8">

window.onload=function () {

  if(typeof(FileReader)=="undefined")

  {

    alert("你的浏览器不支持文件读取")

    document.write("")

  }else

  {

    alert("你的浏览器支持文件读取")

  }

}

  function readText() {

      var file=document.getElementById("file").files[0]

      var reader=new FileReader()

      reader.readAsText(file)

      reader.onload=function(data)

      {

        var tt=document.getElementById("tt")

        tt.innerHTML=this.result

      }

    }

  

</script>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7466125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存