开发者

Replace all & that are not a HTML entity using C#

开发者 https://www.devze.com 2022-12-28 03:44 出处:网络
Basically a dup of this question using php, but I need it for C#. I need to be able to replace any & that is not currently not any HTML entity开发者_如何学C (e.g. &) before outputting to

Basically a dup of this question using php, but I need it for C#.

I need to be able to replace any & that is not currently not any HTML entity开发者_如何学C (e.g. &) before outputting to screen. I was thinking a regex, but I'm not sure if .Net has something built in that will do this.


You can use HttpUtility.HtmlEncode.
Whithing the context of a page or UserControl, you can use Server.HtmlEncode.


Better AntiXss.HtmlEncode, prevents XSS.


You could always HTML Decode the string (which would turn any HTML symbols into their display equivalents), replace any &'s, and then Encode the string again (which turns the symbols back into what they were originally). You might need to watch for side effects though.

0

精彩评论

暂无评论...
验证码 换一张
取 消