Intercepts HTML characters and retains HTML format tags.

Source: Internet
Author: User
Tags truncated

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
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;
Using system. Text. regularexpressions;
Using system. text;

Public partial class test system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String STR = pspan style = 'font-size larger 'Five span style = 'color red' Dalian spanspan style = 'font-family 'pool span wind span style = 'font-family youyuan 'scenic span official website spanspan style = 'font-family 'span style = ''span style = 'font-size larger' Song spanspanspanspan style = 'font-family _ gb2312 'span style = 'font-size larger 'spanspanp;
Response. Write (substringtohtml (STR, 16 ,...));
}

Summary
Truncates a string by byte length (HTML is supported) Code Style string)
Summary
Param name = the string parameter Param to be intercepted by Param
Param name = Length: the length of the truncated bytes. Param
Param name = end: the string Param added at the end of the string
Returns returns the intercepted 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)
{< br> N = n + 1;
Unicode characters occupy two bytes
If (system. text. encoding. default. getbytes (temp + ). length 1)
{< br> N = n + 1;
}< BR >}

Result. append (temp );
If (n = length)
{
Break;
}
}
Result. append (end );
Extract the HTML tag from the truncated string.
String temp_result = result. tostring (). Replace () [^] (), $1 $2 );
Remove HTML tags that do not need to be collocated.
Temp_result = temp_result.replace (@ (kerberodybrcolgroupdddtframeheadhrhtmlimginputisindexlilinkmetaoptionpparamtbodytdtfootththeadtr) [^],
);
Remove the paired HTML Tag
Temp_result = temp_result.replace (@ ([A-Za-Z] +) [^] (.) 1, $2 );
Use regular expressions to retrieve tags
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 ();
}
}

 

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.