email地址处理

示例一

    email = "[email protected]"
    url = email.gsub(/./) { |c| '%' + c.unpack('H2' * c.size).join('%').upcase }
    html = url[1..-1].split(/%/).collect { |c| sprintf("&#%03d;", c.to_i(16)) }.join

    p "<a href=\"mailto:#{url}\">#{html}</a>"
    # => "<a href=\"mailto:%6A%73%6E%74%67%68%66%40%73%69%6E%61%2E%63%6F%6D\">&#106;&#115;&#110;&#116;&#103;&#104;&#102;&#064;&#115;&#105;&#110;&#097;&#046;&#099;&#111;&#109;</a>"

 

示例二

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

 

具体示例:

 

<a href="<%= xml_encode("mailto:" + url_encode("[email protected]")) %>">mail somebody</a>

 

查看源文件:

 

<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>

 

你可能感兴趣的:(C++,c,xml,.net,C#)