处理,输出指定标签的指定属性的属性值,字符组形式
2007-11-14 10:00
393 查看
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// tag_content 的摘要说明
/// </summary>
public class tag_content
{
public tag_content()
{
//TODO: 在此处添加构造函数逻辑
}
/// <summary>
/// 处理,输出指定标签的指定属性的属性值,字符组形式 by:gxtiou
/// </summary>
/// <param name="incontent">输入要处理的内容</param>
/// <param name="tagname">标签名称,如"img"</param>
/// <param name="attri">标签属性名 如"src"</param>
public string[] putout_tag_attri(string incontent, string tagname, string attri)
{
#region
incontent=incontent.ToLower();
tagname=tagname.ToLower();
attri=attri.ToLower();
#endregion
#region
int p_start = 0; //处理字符串起点
int p_end = 0; //处理字符串起点
string got_now = ""; //取到的 <img*> 字符串
string arr_string_out =""; //暂存输出数据
#endregion
#region
while (incontent.Length > 0)
{
p_start = incontent.IndexOf("<"+tagname+" ");
if (p_start == -1) //找不到标签
{
break;
}
else
{
p_end = incontent.IndexOf(">", p_start);
if (p_end != -1)
{
got_now = incontent.Substring(p_start, p_end - p_start+1);
incontent = incontent.Substring(p_end);
p_start = got_now.IndexOf(" " + attri + "=");
if (p_start!=-1)//有该属性时
{
//考虑到属性值以双引号“”包含时
#region
if (got_now.Substring(p_start + attri.Length + 2, 1) == "/"")
{
p_end = got_now.IndexOf("/"", p_start + attri.Length + 3);
if (p_end != -1)
{
got_now = got_now.Substring(p_start + attri.Length + 3, p_end - p_start - attri.Length - 3);
}
else
{
got_now = "";
}
}
#endregion
//考虑到属性值以双引号“”包含时
#region
else if (got_now.Substring(p_start + attri.Length + 2, 1) == "/'")
{
p_end = got_now.IndexOf("/'", p_start + attri.Length + 3);
if (p_end != -1)
{
got_now = got_now.Substring(p_start + attri.Length + 3, p_end - p_start - attri.Length - 3);
}
else {
got_now = "";
}
}
#endregion
//考虑到属性值无包含时,以空格,或者 "/>" 或者 “>”结束时
#region
else {
p_end = got_now.IndexOf(" ", p_start + attri.Length + 2);
p_end = p_end == -1 ? got_now.IndexOf("/>", p_start + attri.Length + 2) : p_end;
p_end = p_end == -1 ? got_now.IndexOf(">", p_start + attri.Length + 2) : p_end;
if (p_end != -1)
{
got_now = got_now.Substring(p_start + attri.Length + 2, p_end - p_start - attri.Length - 2);
}
else
{
got_now = "";
}
}
#endregion
}
if (got_now != "")
{
arr_string_out += "/"" + got_now;
}
}
else//没有结束标记标签
{
break;
}
}
}
#endregion
#region
//获取到了字符串
if (arr_string_out!="")
{
arr_string_out.TrimStart('/"');
return arr_string_out.Split('/"');
}
else
{
return null;
}
#endregion
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// tag_content 的摘要说明
/// </summary>
public class tag_content
{
public tag_content()
{
//TODO: 在此处添加构造函数逻辑
}
/// <summary>
/// 处理,输出指定标签的指定属性的属性值,字符组形式 by:gxtiou
/// </summary>
/// <param name="incontent">输入要处理的内容</param>
/// <param name="tagname">标签名称,如"img"</param>
/// <param name="attri">标签属性名 如"src"</param>
public string[] putout_tag_attri(string incontent, string tagname, string attri)
{
#region
incontent=incontent.ToLower();
tagname=tagname.ToLower();
attri=attri.ToLower();
#endregion
#region
int p_start = 0; //处理字符串起点
int p_end = 0; //处理字符串起点
string got_now = ""; //取到的 <img*> 字符串
string arr_string_out =""; //暂存输出数据
#endregion
#region
while (incontent.Length > 0)
{
p_start = incontent.IndexOf("<"+tagname+" ");
if (p_start == -1) //找不到标签
{
break;
}
else
{
p_end = incontent.IndexOf(">", p_start);
if (p_end != -1)
{
got_now = incontent.Substring(p_start, p_end - p_start+1);
incontent = incontent.Substring(p_end);
p_start = got_now.IndexOf(" " + attri + "=");
if (p_start!=-1)//有该属性时
{
//考虑到属性值以双引号“”包含时
#region
if (got_now.Substring(p_start + attri.Length + 2, 1) == "/"")
{
p_end = got_now.IndexOf("/"", p_start + attri.Length + 3);
if (p_end != -1)
{
got_now = got_now.Substring(p_start + attri.Length + 3, p_end - p_start - attri.Length - 3);
}
else
{
got_now = "";
}
}
#endregion
//考虑到属性值以双引号“”包含时
#region
else if (got_now.Substring(p_start + attri.Length + 2, 1) == "/'")
{
p_end = got_now.IndexOf("/'", p_start + attri.Length + 3);
if (p_end != -1)
{
got_now = got_now.Substring(p_start + attri.Length + 3, p_end - p_start - attri.Length - 3);
}
else {
got_now = "";
}
}
#endregion
//考虑到属性值无包含时,以空格,或者 "/>" 或者 “>”结束时
#region
else {
p_end = got_now.IndexOf(" ", p_start + attri.Length + 2);
p_end = p_end == -1 ? got_now.IndexOf("/>", p_start + attri.Length + 2) : p_end;
p_end = p_end == -1 ? got_now.IndexOf(">", p_start + attri.Length + 2) : p_end;
if (p_end != -1)
{
got_now = got_now.Substring(p_start + attri.Length + 2, p_end - p_start - attri.Length - 2);
}
else
{
got_now = "";
}
}
#endregion
}
if (got_now != "")
{
arr_string_out += "/"" + got_now;
}
}
else//没有结束标记标签
{
break;
}
}
}
#endregion
#region
//获取到了字符串
if (arr_string_out!="")
{
arr_string_out.TrimStart('/"');
return arr_string_out.Split('/"');
}
else
{
return null;
}
#endregion
}
}
相关文章推荐
- 指定HTML标签属性 |Specifying HTML Attributes| 在视图中生成输出URL |高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼
- [原创]java WEB学习笔记41:简单标签之带属性的自定义标签(输出指定文件,计算并输出两个数的最大值 demo)
- css基础 属性选择器 属性值以指定字符开头 / 结束
- 编写程序,删除字符串中指定位置m上开始的n个字符,并输出删除后的字符串。
- 使用jQuery筛选排除元素以修改指定标签的属性
- 字符串是否包含字符,标签是否包含属性,获取表格行列,节点,元素筛选
- Jquery 获取指定标签的对象及属性的设置与移除
- C#截取指定长度字符超出部分以"..."代替,用于处理过长的标题
- 框架之Struts2 处理结果类型、struts.xml标签属性及验证输入的方式
- Action属性注入值和指定Struts 2处理的请求
- [C/C++]_[字符串处理]_[过滤出HTML标签的属性值]
- php 处理 html 标签属性
- PHP扩展之文本处理(二)——PCRE正则表达式语法4——Unicode字符属性
- printf格式化输出时动态指定字符宽度
- ssas处理时找不到属性键,由全角字符引起的。
- 字符串处理 删除指定字符
- JavaScript权威指南_163_第17章_事件处理_17.2-注册事件处理程序-设置HTML标签属性为事件处理程序
- JavaScript查看标签的所有属性和属性值
- struts2 输出指定日期格式标签
- SQL分割字符串,SQL按照指定字符分割字符串,SQL处理字符串...