Merhaba arkadaşlar Asp.net veya .Net platformunu yazılım için kullanan arkadaşlara yardımcı olacağı düşüncesiyle kendim için yazdığım strip_tags fonksiyonumu paylaşıyorum.
http://mustafaturan.net/download/lec...ags_csharp.txt
static string strip_tags(string str, string allowed_tags)
{
/*
// Coder: Mustafa Turan
// Date: 05.09.2008
// Contact:
// http://mustafaturan.net/
// techno surprises
// wm [ #at# ] mustafaturan.net
// Licence: GNU and MIT Licence
// EXAMPLES
// ---> function call1: strip_tags("<a href=\"asdadsadsad.html\">doctor</a> <p>pirasa</p> <img src=\"asd.jpg\" /> <h1>hey you</h1>", "<a>,<p>,<img />")
// ---> result: <a href="">doctor</a> <p>pirasa</p> <img src="asd.jpg" /> hey you
// ---> function call2: strip_tags("<a href=\"asdadsadsad.html\">doctor</a> <p>pirasa</p> <img src=\"asd.jpg\" /> <h1>hey you</h1>", "")
// ---> result: doctor pirasa hey you
*/
// START
// pattern for getting all tags
string pattern_for_all_tags = "</?[^><]+>";
// pattern for allowed tags
string allowed_patterns = "";
if(allowed_tags!=""){
// get allowed tags if any exists
Regex r = new Regex("[\\/<> ]+");
allowed_tags = r.Replace(allowed_tags,"");
string[] allowed_tags_array = allowed_tags.Split(',');
foreach (string s in allowed_tags_array)
{
if (s == "") continue;
// Definin patterns
string p_1 = "<" + s + " [^><]*>$";
string p_2 = "<" + s + ">";
string p_3 = "</" + s + ">";
if(allowed_patterns!="")
allowed_patterns += "|";
allowed_patterns += p_1 + "|" + p_2 + "|" + p_3;
}
}
// Get all html tags included on string
Regex strip_tags = new Regex(pattern_for_all_tags);
MatchCollection all_tags_matched = strip_tags.Matches(str);
if (allowed_patterns != "")
foreach (Match m in all_tags_matched)
{
Regex r_1 = new Regex(allowed_patterns);
Match m_1 = r_1.Match(m.Value);
if (!m_1.Success)
{
// if not allowed replace it
str = str.Replace(m.Value, "");
}
}
else
// if not allow anyone replace all
str = strip_tags.Replace(str, "");
return str;
}
C# ile strip_tags
2
●785