views:

292

answers:

3

I want to convert an e-mail address into HTML Escape Characters as a basic way to try and avoid being harvested by spam-bots. Like mentioned in this question:

When placing email addresses on a webpage do you place them as text like this:

[email protected]

or use a clever trick to try and fool the email address harvester bots? For example:

HTML Escape Characters:

joe.somebody@company.com

I cannot find a .Net method to do so though. The Html.Encode only does the invalid HTML characters such as £$%^& and not letters.

Does a method exist or do I need to write my own?

Many Thanks

+1  A: 

I think you can use

HttpUtility.HtmlEncode

also see here: http://msdn.microsoft.com/en-us/library/73z22y6h.aspx

Philipp G
Yeah. Cheers. But as mentioned this method only does invalid HTML characters and not letters or numbers. It basically has no effect of the e-mail in the html source.
Damien
Oh ok, then I missunderstood you. Sorry.
Philipp G
Np. Welcome to SO by the way :)
Damien
+2  A: 

I'm not aware of anything included in the framework, but you could try something like this?

public string ConvertToHtmlEntities(string plainText)
{
    StringBuilder sb = new StringBuilder(plainText.Length * 6);
    foreach (char c in plainText)
    {
        sb.Append("&#").Append((ushort)c).Append(';');
    }
    return sb.ToString();
}
LukeH
Thanks! So it's $# + numeric code? Excellent.
Damien
+1  A: 

No, but it's easy enough to write one...

Public Shared Function HtmlEncodeAll(ByVal Value As String) As String
    Dim Builder As New StringBuilder()

    For Each Chr As Char In Value
        Builder.Append("&#")
        Builder.Append(Convert.ToInt32(Chr))
        Builder.Append(";")
    Next

    Return Builder.ToString()
End Function
Josh Stodola