您的位置:首页 > 其它

处理,输出指定标签的指定属性的属性值,字符组形式

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

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐