How to convert urls and emails in text file to html?

M

MARTIN LANNY

Hi All,

In my program I am loading the content of a text file (main.txt) into a
string called 'message'.

What I need is to convert all urls and emails in this string into html.

This is what I did to convert hard returns to <br>'s and it works fine:

Dim sws As New IO.StreamWriter(pathtosave)
sws.WriteLine(message.Replace(Chr(13), "<br>"))

So hard returns were easy, but how to do it in case of urls and emails?

So, that in case of url it should change "www.test.com" to "<a
href="http://www.test.com">www.test.com</a>" and in case of email it
should change something like "(e-mail address removed)" to "<a
href="mailto:[email protected]">[email protected]</a>"

Can someone help me out?

Martin
 
H

Herfried K. Wagner [MVP]

MARTIN LANNY said:
What I need is to convert all urls and emails in this string into html.

This is what I did to convert hard returns to <br>'s and it works fine:

Dim sws As New IO.StreamWriter(pathtosave)
sws.WriteLine(message.Replace(Chr(13), "<br>"))

So hard returns were easy, but how to do it in case of urls and emails?

So, that in case of url it should change "www.test.com" to "<a
href="http://www.test.com">www.test.com</a>" and in case of email it
should change something like "(e-mail address removed)" to "<a
href="mailto:[email protected]">[email protected]</a>"

I think that you'll have to implement the code to convert the URLs and
addresses to hyperlinks yourself. If you are dealing heavily with HTML, I
suggest to take a look at the 'HttpUtility' class which provides methods for
encoding URLs and text.
 
M

MARTIN LANNY

Hi, it won't let me use it.

I added:

Imports System.Web
....
..
Dim finalbody As String
finalbody = System.Web.HttpUtility.htmlencode(message)


but "System.Web.htmlencode" is underlined saying that htmlencode is not
a member of system.web.

I also tried: finalbody = HttpUtility.htmlencode(message), but that is
the same.

Why can't I use it?
 
H

Herfried K. Wagner [MVP]

MARTIN LANNY said:
Hi, it won't let me use it.

I added:

Imports System.Web

Make sure your project contains a reference to "System.Web.dll".
 
M

MARTIN LANNY

It doesn't work:

dim message as string = "(e-mail address removed) wrote in message"
Dim finalbody As String
finalbody = System.Web.HttpUtility.HtmlEncode(message)
return finalbody

This is what it returns:
"&lt;[email protected]&gt; wrote in message"

It should return this:

Any other ideas?
 
H

Herfried K. Wagner [MVP]

MARTIN LANNY said:
dim message as string = "(e-mail address removed) wrote in message"
Dim finalbody As String
finalbody = System.Web.HttpUtility.HtmlEncode(message)
return finalbody

This is what it returns:
"&lt;[email protected]&gt; wrote in message"

It should return this:
"<a href="mailto:[email protected]">[email protected]</a> wrote in message"

As I already said, you'll have to build the HTML code yourself.
 
M

MARTIN LANNY

And I guess, that is what I needed a help with initialy.
I don't know how to convert text links to html coded links.
Maybe I wasn't very clear., but this System.Web.HttpUtility.HtmlEncode
didn't do any of what I asked for.
 
H

Herfried K. Wagner [MVP]

MARTIN LANNY said:
And I guess, that is what I needed a help with initialy.
I don't know how to convert text links to html coded links.
Maybe I wasn't very clear., but this System.Web.HttpUtility.HtmlEncode
didn't do any of what I asked for.

For example:

\\\
Private Function EMailAddressToHtml(ByVal Address As String) As String
Return _
"<a href=""mailto:" & HttpUtility.UrlEncode(Address) & """>" & _
HttpUtility.HtmlEncode(Address) & _
"</a>"
End Function
///
 
M

MARTIN LANNY

That is perfect examle, but if I feed my whole text to it, it won't do
anything.
I need some function to scan text for emails and urls and replace them
all.
Isn't regex.replace something I could use? And if yes, how?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top