Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

url and xml encode to fool naive web spiders (See related posts)

Encode all characters to XML entities:

  def xml_encode(text)
    text.unpack('c*').map{|c|"&\##{c};"}.join
  end


Encode all characters to %00%00.. url ecoding:

  def url_encode(text)
    text.split('').map{|c|"%#{c.unpack('H2')}"}.join
  end


The following:

  <a href="<%= xml_encode("mailto:" + url_encode("somebody@somewhere.net")) %>">mail somebody</a>


yields:

  <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#37;&#55;&#51;&#37;&#54;&#102;&#37;&#54;&#100;&#37;&#54;&#53;&#37;&#54;&#50;&#37;&#54;&#102;&#37;&#54;&#52;&#37;&#55;&#57;&#37;&#52;&#48;&#37;&#55;&#51;&#37;&#54;&#102;&#37;&#54;&#100;&#37;&#54;&#53;&#37;&#55;&#55;&#37;&#54;&#56;&#37;&#54;&#53;&#37;&#55;&#50;&#37;&#54;&#53;&#37;&#50;&#101;&#37;&#54;&#101;&#37;&#54;&#53;&#37;&#55;&#52;">mail somebody</a>


which works fine in a browser.

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts