ASP. NET replaces arbitrary code in HTML code

Source: Internet
Author: User

Any code in ASP. NET replacing HTML code <>

Replace any code in <>.
The method is as follows:
Protected string dealto (string procstr)
{
String [] strarr = {"onclick", "onmouseover", "onmouseout", "ondblclick", "ondisposed", "oninit", "onkeydown", "onkeyup ", "onkeypress", "onLoad", "onmousedown", "onmousemove", "onmouseup", "onperender", "onUnload "};
// Code array to be replaced, which can be customized
String replaceto = "RPT ";
String restr = "";
Restr = procstr;
For (INT I = 0; I <strarr. length; I ++)
{
Restr = checkanddeal (restr, strarr [I], replaceto );
}
Return restr;
}

Protected string checkanddeal (string checkstr, string repstr, string replaceto)
{
Int strindex;
Int strlen = 0;

Strlen = checkstr. length;
String returnstr = "";
String tempreplaceto = "asdfghjkl ";
// Temporarily replace the string, which can be replaced by any string, but it must be ensured that the string does not appear in the string to be processed

If (strlen <= 0)
{
Return "";
}
Strindex = checkstr. indexof (repstr );
If (strindex! =-1) // No need to judge if the value is equal to or less than 0. You can directly return the original string.
{
While (checkstr. indexof (repstr )! =-1) // cyclically determine whether the string can be matched and replaced.
{
Int indexofgreat =-1;
Int indexofless =-1;
Strindex = checkstr. indexof (repstr );
Char [] Charr = checkstr. tochararray ();
// Search for characters <and>
Char coms = '';
For (INT I = strindex-1; I> = 0; I --)
{
// String S = checkstr. substring (I, 1 );
Coms = Charr [I];
If (COMS = '> ')
{
Break;
}
Else if (COMS = '<')
{
Indexofless = I;
Break;
}
}
For (INT I = strindex; I <strlen; I ++)
{
// String S = checkstr. substring (I, 1 );
Coms = Charr [I];
If (COMS = '<')
{
Break;
}
Else if (COMS = '> ')
{
Indexofgreat = I;
Break;
}
}
String subleft = "";
String subright = "";
Subleft = checkstr. substring (0, checkstr. indexof (repstr ));
Subright = checkstr. substring (checkstr. indexof (repstr) + repstr. Length ));

If (indexofgreat>-1 & indexofless>-1 & indexofgreat> indexofless & indexofgreat <strlen)
// If yes, replace it with a temporary string. Otherwise, replace the searched string with a temporary string.
{
Checkstr = subleft + replaceto + subright;
}
Else
{
Checkstr = subleft + tempreplaceto + subright;
}
}
Checkstr = checkstr. Replace (tempreplaceto, repstr); // replace the temporary string with the original string.
Returnstr = checkstr;
}
Else
{
Returnstr = checkstr;
}
Return returnstr;
}

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.