Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPs htmlspecialcharacters equivalent in .NET?

Tags:

c#

.net

php

asp.net

PHP has a great function called htmlspecialcharacters() where you pass it a string and it replaces all of HTML's special characters with their safe equivalents, it's almost a one stop shop for sanitizing input. Very nice right?

Well is there an equivalent in any of the .NET libraries?

If not, can anyone link to any code samples or libraries that do this well?

like image 985
Adam Avatar asked Aug 19 '08 19:08

Adam


People also ask

What is HTML special characters PHP?

Description. The htmlspecialchars() function is used to converts special characters ( e.g. & (ampersand), " (double quote), ' (single quote), < (less than), > (greater than)) to HTML entities ( i.e. & (ampersand) becomes &amp, ' (single quote) becomes &#039, < (less than) becomes &lt; (greater than) becomes &gt; ).

What is the difference between Htmlentities and Htmlspecialchars in PHP?

Difference between htmlentities() and htmlspecialchars() function: The only difference between these function is that htmlspecialchars() function convert the special characters to HTML entities whereas htmlentities() function convert all applicable characters to HTML entities.

What is &# 039 in HTML?

&#39 is the HTML character coding for an apostrophe ('), so if you see “don&#39t” or “can&#39t” this means that the words “don't” or “can't” are being represented by ecards.


2 Answers

Try this.

var encodedHtml = HttpContext.Current.Server.HtmlEncode(...);
like image 162
Nick Berardi Avatar answered Sep 30 '22 00:09

Nick Berardi


System.Web.HttpUtility.HtmlEncode(string)

like image 37
Forgotten Semicolon Avatar answered Sep 30 '22 01:09

Forgotten Semicolon