# Detect any "\<a href=mailto:...\>...\</a\>" string in a string?

**URL:** <https://www.ruby-forum.com/t/detect-any-a-href-mailto-a-string-in-a-string/176905>\
**Category:** Ruby\
**Created:** [October 7, 2009, 10:57pm UTC](https://www.ruby-forum.com/t/detect-any-a-href-mailto-a-string-in-a-string/176905 "2009-10-07T22:57:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 7, 2009, 10:57pm UTC](https://www.ruby-forum.com/t/detect-any-a-href-mailto-a-string-in-a-string/176905/1 "2009-10-07T22:57:37Z")

</div>

Hi all

I have a long, long string of HTML tags. There might be some unprotected  
Email links in there like this:

[Some Email](mailto:removed_email_address@domain.invalid)

or

[Some Email](mailto:removed_email_address@domain.invalid?subject=Something)

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. I stumbled upon the TMail GEM and guess it could help me a  
lot… But I don’t get any further now.

Any help is appreciated! Thanks!  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 7, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/detect-any-a-href-mailto-a-string-in-a-string/176905/2 "2009-10-07T23:24:25Z")

</div>

On Wed, Oct 7, 2009 at 3:57 PM, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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. I stumbled upon the TMail GEM and guess it could help me a  
> lot… But I don’t get any further now.

Here’s how I do it in PHP, if you wanna rework it into Ruby:

$GLOBALS[‘EMAIL\_LINK\_REGEX’] = “#\<a[^\>]_mailto:([^'"]_)['"  
]\>([^\<]\*)#i”;

$html = preg\_replace\_callback( $GLOBALS[‘EMAIL\_LINK\_REGEX’],  
‘fubarEmail’, $html );

function fubarEmail( $matches )  
{  
$strNewAddress = replaceEntities( $matches[1] );

$strText = replaceEntities( $matches[2] );

$arrEmail = explode( ‘@’, $strNewAddress );

$strTag = “$arrEmail[0] at \r”;  
$strTag .= str\_replace( ‘.’, ’ dot ', $arrEmail[1] ) .  
‘’;

return $strTag;  
}

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 8, 2009, 7:30am UTC](https://www.ruby-forum.com/t/detect-any-a-href-mailto-a-string-in-a-string/176905/3 "2009-10-08T07:30:25Z")

</div>

Joshua M. 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](mailto:removed_email_address@domain.invalid)
> 
> or
> 
> [Some Email](mailto:removed_email_address@domain.invalid?subject=Something)
> 
> 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 page [Some Email](mailto:removed_email_address@domain.invalid)
hello

world

goodbye
[Some Email](mailto:removed_email_address@domain.invalid?subject=Something&cost=10) ENDOFHTML

new\_html = html.gsub(/(.+?)\</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:removed\_email\_address@domain.invalid"\>Some Email”  
“[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”  
“Some Email”

“\<a href="mailto:removed\_email\_address@domain.invalid?subject=Something&cost=10"\>Some  
Email”  
“[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)?subject=Something&cost=10”  
“Some Email”  
there is a query string to parse  
[“subject=Something”, “cost=10”]

 html page the replacement string cobbled together from the pieces above 
hello

world

goodbye
 the replacement string cobbled together from the pieces above

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 8, 2009, 8:07am UTC](https://www.ruby-forum.com/t/detect-any-a-href-mailto-a-string-in-a-string/176905/4 "2009-10-08T08:07:30Z")

</div>

Joshua M. wrote:

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

By the way, you can’t substitute js functions for tags.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 11, 2009, 12:16pm UTC](https://www.ruby-forum.com/t/detect-any-a-href-mailto-a-string-in-a-string/176905/5 "2009-10-11T12:16:57Z")

</div>

Daniel Danopia wrote:

> On Oct 7, 5:19ï¿½pm, Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > or…  
> > > Here’s how I do it in PHP, if you wanna rework it into Ruby:
> > 
> > ï¿½ $strTag .= “document.write(‘$arrEmail[1]"\>’);\r”;  
> > Greg D.[http://destiney.com/](http://destiney.com/)  
> > You could also use a library such as hpricot or nokogiri to search and  
> > replace all the tags.
> 
>  
> 
> And you should have \r\n, not \r, if you are writing HTML.

 

Thank you guys. Nokogiri looks really useful, I will take a look at it.  
🙂

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 8, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/detect-any-a-href-mailto-a-string-in-a-string/176905/6 "2009-10-08T13:01:01Z")

</div>

On Oct 7, 5:19 pm, Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > or…  
> > Here’s how I do it in PHP, if you wanna rework it into Ruby:
> 
> $strTag .= “document.write(‘$arrEmail[1]"\>’);\r”;  
> Greg D.[http://destiney.com/](http://destiney.com/)  
> You could also use a library such as hpricot or nokogiri to search and  
> replace all the tags.

 

And you should have \r\n, not \r, if you are writing HTML.
