开发者

Matching a URL Encoded e-mail address in C#

I did some searching and didn't quite figure out why my solution is not working. Basically I need to take a string (which is HTML code) parse it and look for mailto links (which I then want to replace as part of an obfuscation). Here is what I have thus far:

    string text = "<p>Some Person<br /> Person's Position<br />p. 123-456-7890<br /> e. <a  title=\"Email Some Person\" target=\"_blank\" href=\"mailto:someperson%40domain.com\">someperson@domain.com</a></p>";
    text = Server.UrlDecode(text);
    string safeEmails = Regex.Replace(text, "(<a href=\"mailto:)(.*?)(%40)(.*?)(\">)(.*?)(</a>)", "<a class=\"mailme\" href=\"$2*$4\">$6</a>");
    Response.Write( Server.HtmlDecode(safeEmails));

The text is coming out of a WYSIWYG text editor (Telrik RadEditor for those familiar) and for all intents and purposes I don't have access to be able to control what is coming out of it.

Basically I need to find and replace any:

<a href="mailto:someone%40domain.com">someone@domain.com</a>

With:

<a class="mailme" href="someone@domain.com开发者_如何学JAVA">someone@domain.com</a>

Some background: I am attempting to create a mailto link that will avoid detection by harvesters. The problem is that I receive a string with the e-mail as a standard mailto link. I cannot control the incoming string, so the mailto will always be an unprotected mailto. My object is to find all of them, obfuscate them, then use JavaScript to "fix" the link so that human vistors can easily use the mailto links. I am open to new approaches as well as modifications to the above code.


You could use a regex or the HTML agility pack to find and obfuscate all your mailto. If you want a good obfuscation try reading ten methods to obfuscate e-mail addresses compared

EDIT: sorry, from the first version of your question I didn't get you had a problem in making your regex work. Since you're usign a WYSIWYG text editor, I think the HTML that comes out of it should be pretty "regular", so you may be fine using a regex. You can try changing your Replace line like this:

string safeEmails = Regex.Replace(text, "href=\"mailto:.*\">(.*)</a>", "class=\"mailme\" href=\"$1\">$1</a>");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜