获取一段HTML文本中的第一张图片与截取内容摘要
有时候我们获得到的数据是一段HTML文本,也许这段文本里面有许多图片,需要截取一张作为标题图片,这时就可以用到下面这个方法获取到第一张图片:
#region 获取第一张图片
/// <summary>
/// 获取HTML文本的图片地址
/// </summary>
/// <param name="content"></param>
/// <returns></returns>/
///
public ArrayList getimgurl(string html)
{
ArrayList resultStr = new ArrayList();
Regex r = new Regex(@"<IMG[^>]+src=\s*(?:'(?<src>[^']+)'|""(?<src>[^""]+)""|(?<src>[^>\s]+))\s*[^>]*>", RegexOptions.IgnoreCase);//忽视大小写
MatchCollection mc = r.Matches(html);
foreach (Match m in mc)
{
resultStr.Add(m.Groups["src"].Value.ToLower());
}
if (resultStr.Count > 0)
{
return resultStr;
}
else
{
resultStr.Clear();
return resultStr;
}
}
#endregion
注意:上面所返回的是一个ArrayList 集合,包含了文本里面所有的Img的src!
这样我们就可以访问到img的src了。
有时候我们得到的数据是一段HTML文本,需要截取HTML文本的一部分作为内容摘要。此时,我们可以运用下面这个方法:
#region 新闻内容摘要
/// <summary>
/// 新闻内容摘要
/// </summary>
/// <param name="sString"></param>
/// <param name="nLeng"></param>
/// <returns></returns>
public string GetContentSummary(string content, int length, bool StripHTML)
{
if (string.IsNullOrEmpty(content) || length == 0)
return "";
if (StripHTML)
{
Regex re = new Regex("<[^>]*>");
content = re.Replace(content, "");
content = content.Replace(" ", "").Replace(" ", "");
if (content.Length <= length)
return content;
else
return content.Substring(0, length) + "……";
}
else
{
if (content.Length <= length)
return content;
int pos = 0, npos = 0, size = 0;
bool firststop = false, notr = false, noli = false;
StringBuilder sb = new StringBuilder();
while (true)
{
if (pos >= content.Length)
break;
string cur = content.Substring(pos, 1);
if (cur == "<")
{
string next = content.Substring(pos + 1, 3).ToLower();
if (next.IndexOf("p") == 0 && next.IndexOf("pre") != 0)
{
npos = content.IndexOf(">", pos) + 1;
}
else if (next.IndexOf("/p") == 0 && next.IndexOf("/pr") != 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
sb.Append("<br/>");
}
else if (next.IndexOf("br") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
sb.Append("<br/>");
}
else if (next.IndexOf("img") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
size += npos - pos + 1;
}
}
else if (next.IndexOf("li") == 0 || next.IndexOf("/li") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
}
else
{
if (!noli && next.IndexOf("/li") == 0)
{
sb.Append(content.Substring(pos, npos - pos));
noli = true;
}
}
}
else if (next.IndexOf("tr") == 0 || next.IndexOf("/tr") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
}
else
{
if (!notr && next.IndexOf("/tr") == 0)
{
sb.Append(content.Substring(pos, npos - pos));
notr = true;
}
}
}
else if (next.IndexOf("td") == 0 || next.IndexOf("/td") == 0)
{
npos = content.IndexOf(">", pos) + 1;
if (size < length)
{
sb.Append(content.Substring(pos, npos - pos));
}
else
{
if (!notr)
{
sb.Append(content.Substring(pos, npos - pos));
}
}
}
else
{
npos = content.IndexOf(">", pos) + 1;
sb.Append(content.Substring(pos, npos - pos));
}
if (npos <= pos)
npos = pos + 1;
pos = npos;
}
else
{
if (size < length)
{
sb.Append(cur);
size++;
}
else
{
if (!firststop)
{
sb.Append("……");
firststop = true;
}
}
pos++;
}
}
return sb.ToString();
}
}
#endregion
注意:方法中的bool StripHTML参数表示是否以HTMl文本方式输出,如果为True的话表示去除HTML标签与样式,截取到的是纯文本,反之就是以HTMl文本输出。
这样我们就可以根据自己喜欢的方式来输出文本。
获取一段HTML文本中的第一张图片与截取内容摘要的更多相关文章
- 自动获取wordpress日志中的第一张图片作为缩略图
图片在博客中算是吸引访客阅读欲望的一种方法,在日志列表如果有一张吸引力十足的图片作为缩略图,70%的游客会点击浏览具体的文章.既然那样,赶紧去加缩略图吧. 我们知道 WordPress 有个日志缩略图 ...
- c语言,strcspn,在串中查找第一个给定字符集内容的段
函数名: strcspn 功 能: 在串中查找第一个给定字符集内容的段 用 法: int strcspn(char *str1, char *str2); 程序例: #include <stdi ...
- JavaScript获取一段html片段中a标签的href值
最近,做项目中有一个需求,页面中有一个文本编辑器,里面写的内容最后生成了html代码片段,在另一个页面需要前一个页面文本编辑器的html代码片段中的a标签的href值,就尝试做了,因为不太熟悉js,所 ...
- SpringMVC请求使用@PathVariable获取文件名称并且文件名中存在.导致路径被截取的问题
在SpringMVC中,当使用@pathVariable通过Get请求获取路径名称时,如果路径名称上存在小数点,则获取不到小数点后面的内容,会被Spring截取. 比如我获取某一文件,路径是local ...
- C#中怎样获取默认配置文件App.config中配置的键值对内容
场景 在新建一个程序后,项目中会有一个默认配置文件App.config 一般会将一些配置文件信息,比如连接数据库的字符串等信息存在此配置文件中. 怎样在代码中获取自己配置的键值对信息. 注: 博客主页 ...
- Java利用PushbackReader实现返回对文本中的指定字符串之前的内容
import java.io.FileReader; import java.io.PushbackReader; public class PushbackTest { public static ...
- JavaScript获取select下拉框中的第一个值
JavaScript获取select下拉框中的第一个值 1.说明 获取select下拉框中的第一个值 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- KMP算法 --- 在文本中寻找目标字符串
KMP算法 --- 在文本中寻找目标字符串 很多时候,为了在大文本中寻找到自己需要的内容,往往需要搜索关键字.这其中就牵涉到字符串匹配的算法,通过接受文本和关键词参数来返回关键词在文本出现的位置.一般 ...
- 织梦CMS调用文章第一张图片(非缩略图)终极方法
之前,网上流传了很多在织梦CMS中调用第一张图片的方法,但大体都一样.即删除缩略图字符串,并添加后缀.然而这种方法仅限于jpg图片或其他单独图片类的调用.如果一个站有png.JPG.gif等多种格式. ...
随机推荐
- Codeforces Beta Round #25 (Div. 2 Only)E. Test
E. Test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- KMP 、扩展KMP、Manacher算法 总结
一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...
- vue中手机号,邮箱正则验证以及60s发送验证码
今天写了一个简单的验证,本来前面用的组件,但是感觉写的组件在此项目不是很好用,由于用到的地方比较少,所以直接写在了页面中.页面展示如图 <div> <p class=&quo ...
- android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)
在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄很清楚. 现在把整个总结一下. 其实只要把下面这张图看明白就没问题了. 涉及到的方法一共有下面几个: view获取自身 ...
- Apriori算法实例
Apriori算法与实例 R. Agrawal 和 R. Srikant于1994年在文献[2]中提出了Apriori算法,该算法的描述如下: 下面是一个具体的例子,最开始数据库里有4条交易,{A.C ...
- RPi 2B DDNS 动态域名
/**************************************************************************** * RPi 2B DDNS 动态域名 * 说 ...
- ng2中文文档地址
https://angular.cn/docs/ts/latest/guide/displaying-data.html
- HTTP node静态资源请求加载demo
MIME type的缩写为(Multipurpose Internet Mail Extensions)代表互联网媒体类型(Internet media type),MIME使用一个简单的字符串组成, ...
- 国外1.5免费空间000webhost申请方法
空间大小:1500M 支持语言:PHP 数 据 库:MYSQL 国家/地区:国外 申请地址:http://www.000webhost.com/ 1500M/100GB/PHP/MYSQL/FTP ...
- centos安装xen虚拟机并且配置bridge
主要参考的几个官方文档: http://wiki.centos.org/HowTos/Xen/Xen4QuickStart 在centos上安装xen组件并建立dom0 http://wiki.cen ...