Convert Textbox.Text to HTML

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
I have a form which I allows someone to compile an email in a Textbox. It must include carriage returns. I am trying to use this to encode the textbox into an html format.

Code:
Imports System.Net
Imports System.Web

Code:
    Public Function ToHtml(s As String, nofollow As Boolean) As String
        s = [B]HttpUtility[/B].HtmlEncode(s)
        Dim paragraphs As String() = s.Split(New String() {vbCr & vbLf & vbCr & vbLf}, StringSplitOptions.None)
        Dim sb As New StringBuilder()
        For Each par As String In paragraphs
            sb.AppendLine("<p>")
            Dim p As String = par.Replace(Environment.NewLine, "<br />" & vbCr & vbLf)
            If nofollow Then
                p = Regex.Replace(p, "\[\[(.+)\]\[(.+)\]\]", "<a href=""$2"" rel=""nofollow"">$1</a>")
                p = Regex.Replace(p, "\[\[(.+)\]\]", "<a href=""$1"" rel=""nofollow"">$1</a>")
            Else
                p = Regex.Replace(p, "\[\[(.+)\]\[(.+)\]\]", "<a href=""$2"">$1</a>")
                p = Regex.Replace(p, "\[\[(.+)\]\]", "<a href=""$1"">$1</a>")
                sb.AppendLine(p)
            End If
            sb.AppendLine("</p>")
        Next
        Return sb.ToString()
    End Function


The bold bit is where I'm having a problem. I have added a reference to the System.Web as well. It still will not allow me to use HttpUtility.

Just to add, if anyone knows of a quicker way to achieve what I'm trying to do then I am all ears!
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
C# version for clarity for other programmers here.

Code:
public string ToHtml(string s, bool nofollow)
{
	s = HttpUtility.HtmlEncode(s);
	string[] paragraphs = s.Split(new string[] { Constants.vbCr + Constants.vbLf + Constants.vbCr + Constants.vbLf }, StringSplitOptions.None);
	StringBuilder sb = new StringBuilder();
	foreach (string par in paragraphs) {
		sb.AppendLine("<p>");
		string p = par.Replace(Environment.NewLine, "<br />" + Constants.vbCr + Constants.vbLf);
		if (nofollow) {
			p = Regex.Replace(p, "\\[\\[(.+)\\]\\[(.+)\\]\\]", "<a href=\"$2\" rel=\"nofollow\">$1</a>");
			p = Regex.Replace(p, "\\[\\[(.+)\\]\\]", "<a href=\"$1\" rel=\"nofollow\">$1</a>");
		} else {
			p = Regex.Replace(p, "\\[\\[(.+)\\]\\[(.+)\\]\\]", "<a href=\"$2\">$1</a>");
			p = Regex.Replace(p, "\\[\\[(.+)\\]\\]", "<a href=\"$1\">$1</a>");
			sb.AppendLine(p);
		}
		sb.AppendLine("</p>");
	}
	return sb.ToString();
}
 

Darko

Senior Member
Joined
Jul 9, 2008
Messages
627
If you can use jQuery, just pass the element into a variable.

So var htmlEncoded = $("#textboxID");

htmlEncoded then becomes your object.
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,886
Found an easier way.

Code:
    'format the body to include carriage returns
    Private Function textAreaFormat(ByVal theString As String)
        Dim workingString As String = theString
        Dim outputString As String
        workingString = theString
        workingString = Replace(workingString, Strings.Chr(13), "<br>", 1)

        workingString = Replace(workingString, Chr(32), "&nbsp;", 1)
        outputString = workingString
        Return outputString

    End Function
 
Top