Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart HTML encoding

I'm looking for the best way to do some sort of "smart" HTML encoding. For instance:

From: <a>Next >></a> to: <a>Next gt;gt;</a>
From: <p><a><b><< Prev</b></a><br/><a>Next >></a></p> to: <p><a><b>&lt;&lt; Prev</b></a><br/><a>Next gt;gt;</a></p>

So only the non XML / HTML part of the text would be encoded as if HtmlEncode is called.

Any suggestions?

EDIT: This should be as lightweight as possible. The incoming text will come from users which have no knowledge of HTML encoding.

like image 511
Drejc Avatar asked Mar 19 '26 02:03

Drejc


2 Answers

Yes: don’t ever write HTML into your source code. Instead work with an API like DOM that takes care of all encoding issues for you.

like image 157
Bombe Avatar answered Mar 21 '26 14:03

Bombe


If you want a solid and totally reliable C# solution (but heavy-weight) then I'd use the HTML Agility Pack library. You could then iterate through nodes and HTML encode the contents. It's a bit more bullet-proof than regular expressions, but obviously more intense.

If you want to do it client-side, then use JQuery. See Encode HTML entities with jQuery.

like image 37
Dan Diplo Avatar answered Mar 21 '26 16:03

Dan Diplo