I want to converter all special characters into Html encoded characters.
I found many post related to used HttpUtility.HtmlEncode();
,
but it's only convert some of special characters like "&", "<", ">"
.
Is there any way to convert all special character like "š","Ø","þ","›","Ù"
into Html entity using C# or javascript?
replace(/&/g, "&"). replace(/>/g, ">"). replace(/</g, "<"). replace(/"/g, """);
HTML encoding ensures that text will be correctly displayed in the browser, not interpreted by the browser as HTML. For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as an opening or closing bracket of an HTML tag.
The Microsoft AntiXss Library can accomplish this;
string p = Microsoft.Security.Application.Encoder.HtmlEncode("aaa <b>sdf</b> š,Ø,þ,›,Ù", true);
Response.Write(p);
For
aaa <b>sdf</b> š,Ø,þ,›,Ù
you can also do as follows without AntiXSS
public static string HtmlEncode (string text)
{
string result;
using (StringWriter sw = new StringWriter())
{
var x = new HtmlTextWriter(sw);
x.WriteEncodedText(text);
result = sw.ToString();
}
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With