asp.net(文章截取前几行作为列表摘要)无损返回HTML代码
程序员文章站
2024-03-05 10:08:54
复制代码 代码如下:
/* 时间很久远了...
/* 时间很久远了...
复制代码 代码如下:
<br> /* <strong>时间很久远了,有码友要,我抄了下来,记得使用的时候还是有部分例外情况要处理,大部分是ok的。</strong>
* 函数:
* 说明:无损返回html代码
*
* 作者:卡斯kas(qq:10590916)
* 修改:2010-12-13、
*/
public static string lefth(string str, int l)
{
//为了防止错误
string odstr = str;
bool ishtml = false;//判断截取开头是否是字符串
int maxlen = str.length;
int n = 0, i = 0, b = 0, e = 0;
string c, c2 = "", start = "", endlabel = "", alllabel = "";
if (l >= str.length) return str;//截取字段大于字符长度
bool iserr = false;
try
{
while (n < l && i < maxlen)
{
//1
i++;
c = str.substring(i - 1, 1);
string end2str = "";
try
{
end2str = str.substring(i - 1, 2);
}
catch
{
end2str = "";
}
if (c == "<" && end2str != "</")
{
ishtml = true;
b = i;//记录一个位置
//5 解决非成对标签
string end2tag = "";
try
{
end2tag = str.substring(i, 2);
}
catch
{
end2tag = "";
}
if (end2tag == "br")
{
ishtml = false;
}
if (str.substring(i, 1).tolower() == "%")
{
ishtml = false;
}
if (str.substring(i, 1).tolower() == "?")
{
ishtml = false;
}
else if (end2tag == "hr")
{
ishtml = false;
}
//5end
}
//1end
//2
if (c == "<" && end2str == "</")
{
c2 = str.substring(i - 1, str.substring(i - 1).indexof(">") + 1);
alllabel = alllabel.substring(c2.length);
}
//2end
start = start + c;//记录当前字符及其前面字符
if (!ishtml)
{
//6 较为准确的计数
try
{
if (str.substring(i, 2).tolower() == "br")
{
}
else if (str.substring(i, 2).tolower() == "hr")
{
}
else
{
n++;
}
}
catch
{
//如果出错,则返回纯文本(@"<[^>]+>|</[^>]+>");
//odstr = regex.replace(odstr, @"<[^>]+>|</[^>]+>", "", regexoptions.ignorecase);
odstr = odstr.replace("<", "<").replace(">", ">");
if (l > odstr.length) l = odstr.length;
return odstr.substring(0, l);
}
//6end
}
else
{
if (c == ">")//如果循环到>时,要处理的成对标签中间部分
{
if (ishtml)
{
endlabel = str.substring(b, i - b);//获取<到>之间的代码
e = endlabel.indexof(" ");//标签属性都有空格,以空格为分割,获取标签名
if (e > 0)
{
endlabel = "</" + endlabel.substring(0, e) + ">";
}
else
{
endlabel = "</" + endlabel;
}
alllabel = endlabel + alllabel;//记录当前结束标签,及所有有关结束标签
}
ishtml = false;//重置属性,从新开始
}
}
//4 当结束时,发现有未闭合的标签,则重新来一次
if (n + 1 >= l)
{
if (instrn(start, "<") != instrn(start, ">"))
{
n--;
}
}
//4end
}//end while
}
catch
{
iserr = true;
}
string newrestring = start + alllabel + "...";
//最后再次验证
regex rg = new regex("<.[^>/]+>", regexoptions.compiled);
regex rg2 = new regex(@"<\s*/[a-z]\s*>", regexoptions.compiled);
if (instrlen(newrestring, "<") != instrlen(newrestring, ">"))
{
iserr = true;
}
else if (instrlen(newrestring.replace(""", "\""), "\"") % 2 == 1)
{
iserr = true;
}
else if (rg.matches(newrestring).count != rg2.matches(newrestring).count)
{
iserr = true;
}
if (iserr)
{
odstr = odstr.replace("<", "<").replace(">", ">").replace("\"", """).replace("'", "'");
if (l > odstr.length)
{ newrestring = odstr; }
else
{
try
{
newrestring = odstr.substring(0, l) + "...";
}
catch
{
newrestring = odstr;
}
}
}
return newrestring;//返回
}