views:

85

answers:

3

Is there any function that converts

<a href="test.php"/>

to

&lt; href=&quot;test.php&quot;/&gt;

It would be helpful if all html characters were converted in the similar manner.

Is there a function or library to do this?

A: 

use HttpUtility

Nullstr1ng
I need to do this with Actionscript! So, looking for a AS3 library.
Yeti
+2  A: 
var eaten:String = myString.replace(/&/g,'&amp;').
                            replace(/</g,'&lt;').
                            replace(/>/g,'&gt;').
                            replace(/"/g,'&quot;');
Delan Azabani
I was actually looking for something which does that for all the symbols here: http://turner.faculty.swau.edu/webstuff/htmlsymbols.html
Yeti
You don't need to. Those characters aren't parsed (only the above four are) and you should be able to use the direct UTF-8 without escaping them.
Delan Azabani
This seems to replace only the first instance of the character.
Yeti
MrKishi
@Kishi: Why? o_O? Regex is much slower
M28
@M28 It's not, really, given the circunstances. He needs to replace _all_ the special characters, _String.replace(String, String)_ will replace only the first match.
MrKishi
Well, I thought that its behaviour was only in javascript.
M28
True. It seems to replace only the first (which I thought against). Updating the post with a regex...
Delan Azabani
A: 

It sounds like encodeURIComponent may be what you're after:

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/package.html#encodeURIComponent%28%29

Andrew
encodeURIComponent converts <a href="test.php"/> to **%3Ca%20href%3D%22test.php%22%2F%3E**. Oops!
Yeti