Copy codeThe Code is as follows: <BR>/* <STRONG> it has been a long time. I copied the code. I remember that some exceptions should be handled when using it, most of them are OK. </STRONG>
* Function:
* Description: lossless HTML code returned
*
* Author: Kas (QQ: 10590916)
* Modification: 2010-12-13,
*/
Public static string LeftH (string str, int l)
{
// To prevent errors
String odstr = str;
Bool isHtml = false; // determines whether the start of the truncation is a string.
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; // The truncated field is longer than the character Length.
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; // record a location
// 5 resolve unpaired tags
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; // record the current character and its prefix
If (! IsHtml)
{
// 6 accurately count
Try
{
If (str. Substring (I, 2). ToLower () = "br ")
{
}
Else if (str. Substring (I, 2). ToLower () = "hr ")
{
}
Else
{
N ++;
}
}
Catch
{
// If an error occurs, plain text is returned (@ "<[^>] + >|</[^>] +> ");
// 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 the loop is>, the intermediate part of the paired tag to be processed
{
If (isHtml)
{
EndLabel = str. Substring (B, I-B); // obtain the code between <to>
E = EndLabel. IndexOf (""); // tag attributes are separated by spaces to obtain the tag name.
If (e> 0)
{
EndLabel = "</" + EndLabel. Substring (0, e) + "> ";
}
Else
{
EndLabel = "</" + EndLabel;
}
AllLabel = EndLabel + AllLabel; // record the current end label and all related end labels
}
IsHtml = false; // reset the attribute to start from
}
}
// 4. When an unclosed tag is found at the end, the tag is retried.
If (n + 1> = l)
{
If (Limit N (Start, "<")! = Nth N (Start, "> "))
{
N --;
}
}
// 4end
} // End while
}
Catch
{
IsErr = true;
}
String newReString = Start + AllLabel + "...";
// Verify again
Regex Rg = new Regex ("<. [^>/] +>", RegexOptions. Compiled );
Regex Rg2 = new Regex (@ "<\ s */[a-z] \ s *>", RegexOptions. Compiled );
If (FIG (newReString, "<")! = Jsonlen (newReString, "> "))
{
IsErr = true;
}
Else if (FIG (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; // return
}