Joshua Muheim wrote:
> Hi all
> 
> I have a long, long string of HTML tags. There might be some unprotected
> Email links in there like this:
> 
> Some Email
> 
> or
> 
> Some Email
> 
> or...
> 
> I need to detect these email links and substitute them with something
> different, like a JavaScript function to obfuscate them:
> 
> obfuscate("some","email.xx","Something","Some Email")
> 
> or something like that. Sadly I have no idea how to find the needed
> string parts. 

1)regexes
2)gsub()
3)split()


html =<<ENDOFHTML
<html>
<head>
  <title>html page</title>
</head>
<body>
  Some Email
  <div>hello</div>
  <div>world</div>
  <div>goodbye</div>
  <a href="mailto:some / email.xx?subject=Something&cost=10">Some 
Email</a>
</body>
</html>
ENDOFHTML

new_html = html.gsub(/<a href="(.+?)">(.+?)<\/a>/) do |match|
  p match
  addy = $1
  link = $2
  p addy, link

  pieces = addy.split("?")
  if pieces.length == 2
    puts "there is a query string to parse"
    name_vals = pieces[1].split("&")
    p name_vals
  end

  puts

  "the replacement string cobbled together from the pieces above"
end

puts new_html


--output:--
"<a href=\"mailto:some / email.xx\">Some Email</a>"
"mailto:some / email.xx"
"Some Email"

"<a href=\"mailto:some / email.xx?subject=Something&cost=10\">Some 
Email</a>"
"mailto:some / email.xx?subject=Something&cost=10"
"Some Email"
there is a query string to parse
["subject=Something", "cost=10"]

<html>
<head>
        <title>html page</title>
</head>
<body>
  the replacement string cobbled together from the pieces above
        <div>hello</div>
        <div>world</div>
        <div>goodbye</div>
  the replacement string cobbled together from the pieces above
</body>
</html>

-- 
Posted via http://www.ruby-forum.com/.