SoFunction
Updated on 2025-03-10

(The article intercepts the first few lines as list summary) Lossless return HTML code


<BR>         /* <STRONG>It's been a long time since I have a code friend, so I copied it down. I remember that there are some exceptions to deal with when using it, most of which are OK. </STRONG>
* Function:
* Description: Losslessly return HTML code
        *      
* 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;//Judge whether the beginning of the intercept is a string
            int maxLen = ;
            int n = 0, i = 0, b = 0, e = 0;
            string c, c2 = "", Start = "", EndLabel = "", AllLabel = "";
if (l >= ) return str;//The intercept field is greater than the character length

            bool isErr = false;
            try
            {
            while (n < l && i < maxLen)
            {
                //1
                i++;
                c = (i - 1, 1);

                string end2str = "";
                try
                {
                    end2str = (i - 1, 2);
                }
                catch
                {
                    end2str = "";
                }
                if (c == "<" && end2str != "</")
                {
                    isHtml = true;
b = i;//Record a position
//5

                    string end2tag = "";
                    try
                    {
                        end2tag = (i, 2);
                    }
                    catch
                    {
                        end2tag = "";
                    }

                    if (end2tag == "br")
                    {
                        isHtml = false;
                    }
                    if ((i, 1).ToLower() == "%")
                    {
                        isHtml = false;
                    }
                    if ((i, 1).ToLower() == "?")
                    {
                        isHtml = false;
                    }
                    else if (end2tag == "hr")
                    {
                        isHtml = false;
                    }
                    //5end
                }
                //1end

                //2
                if (c == "<" && end2str == "</")
                {
                    c2 = (i - 1, (i - 1).IndexOf(">") + 1);
                    AllLabel = ();
                }
                //2end

Start = Start + c;//Record the current character and its previous characters

                if (!isHtml)
                {
//6
                    try
                    {
                        if ((i, 2).ToLower() == "br")
                        {
                        }
                        else if ((i, 2).ToLower() == "hr")
                        {
                        }
                        else
                        {
                            n++;
                        }
                    }
                    catch
                    {
//If an error occurs, return plain text (@"<[^>]+>|</[^>]+>");
                        //odstr = (odstr, @"<[^>]+>|</[^>]+>", "", );
                        odstr = ("<", "<").Replace(">", ">");
                        if (l > ) l = ;
                        return (0, l);
                    }
                    //6end
                }
                else
                {
if (c == ">")//If looping to >, the middle part of the paired label to be processed
                    {
                        if (isHtml)
                        {
EndLabel = (b, i - b);//Get the code between < to >
e = (" ");//The label attributes have spaces, divided by spaces to get the label name
                            if (e > 0)
                            {
                                EndLabel = "</" + (0, e) + ">";
                            }
                            else
                            {
                                EndLabel = "</" + EndLabel;
                            }
AllLabel = EndLabel + AllLabel;//Record the current end label and all relevant end labels
                        }
isHtml = false;//Reset the property and start again
                    }
                }
//4
                if (n + 1 >= l)
                {
                    if (InStrN(Start, "<") != InStrN(Start, ">"))
                    {
                        n--;
                    }
                }
                //4end
            }//end while

            }
            catch
            {
                isErr = true;
            }

            string newReString = Start + AllLabel + "...";

//Finally verify again
            Regex Rg = new Regex("<.[^>/]+>", );
            Regex Rg2 = new Regex(@"<\s*/[a-z]\s*>", );

            if (inStrLen(newReString, "<") != inStrLen(newReString, ">"))
            {
                isErr = true;
            }
            else if (inStrLen((""", "\""), "\"") % 2 == 1)
            {
                isErr = true;
            }
            else if ((newReString).Count != (newReString).Count)
            {
                isErr = true;
            }
            if (isErr)
            {
                odstr = ("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'");
                if (l > )
                { newReString = odstr; }
                else
                {
                    try
                    {
                        newReString = (0, l) + "...";
                    }
                    catch
                    {
                        newReString = odstr;
                    }
                }
            }

return newReString;//Return
        }