Captures the specified length of html content and retains the html format mark (c)

Source: Internet
Author: User

/// <Summary>
/// Truncate a string by byte length (supports truncate a string with HTML code style)
/// </Summary>
/// <Param name = "param"> string parameter to be truncated </param>
/// <Param name = "length"> truncated bytes </param>
/// <Param name = "end"> string added at the end of the string </param>
/// <Returns> returns the truncated string </returns>
Public string SubstringToHTML (string param, int length, string end)
{
String Pattern = null;
MatchCollection m = null;
StringBuilder result = new StringBuilder ();
Int n = 0;
Char temp;
Bool isCode = false; // is it HTML code?
Bool isHTML = false; // whether it is a special HTML character, such as & nbsp;
Char [] pchar = param. ToCharArray ();
For (int I = 0; I <pchar. Length; I ++)
{
Temp = pchar [I];
If (temp = '<')
{
IsCode = true;
}
Else if (temp = '&')
{
IsHTML = true;
}
Else if (temp = '>' & isCode)
{
N = n-1;
IsCode = false;
}
Else if (temp = ';' & isHTML)
{
IsHTML = false;
}

If (! IsCode &&! IsHTML)
{
N = n + 1;
// UNICODE characters in two bytes
If (System. Text. Encoding. Default. GetBytes (temp + ""). Length> 1)
{
N = n + 1;
}
}

Result. Append (temp );
If (n> = length)
{
Break;
}
}
Result. Append (end );
// Retrieve the HTML tag from the truncated string
String temp_result = result. ToString (). Replace ("(>) [^ <>] * (<?)", "$1 $2 ″);
// Remove the HTML Tag that does not need the element tag
Temp_result = temp_result.Replace (@ "</? (AREA | BASE | BASEFONT | BODY | BR | COL | COLGROUP | DD | DT | FRAME | HEAD | HR | HTML

| IMG | INPUT | ISINDEX | LI | LINK | META | OPTION | P | PARAM | TBODY | TD | TFOOT | TH | THEAD

| TR | area | base | basefont | body | br | col | colgroup | dd | dt | frame | head | hr | html | img | input | isindex | li | link | meta

| Option | p | param | tbody | td | tfoot | th | thead | tr) [^ <>] */?> ",
"");
// Remove the paired HTML Tag
Temp_result = temp_result.Replace (@ "<([a-zA-Z] +) [^ <>] *> (.*?) </1> "," $2 ″);
// Use a regular expression to retrieve a tag
Pattern = ("<([a-zA-Z] +) [^ <>] *> ");
M = Regex. Matches (temp_result, Pattern );
ArrayList endHTML = new ArrayList ();
Foreach (Match mt in m)
{
EndHTML. Add (mt. Result ("$1 ″));
}
// Incomplete HTML tags
For (int I = endHTML. Count-1; I> = 0; I -)
{
Result. Append ("</");
Result. Append (endHTML [I]);
Result. Append ("> ");
}
Return result. ToString ();
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.