Click here to Skip to main content
16,011,626 members
Home / Discussions / Web Development
   

Web Development

 
General.NET Server RC1: Weird Pin
Daniel Turini18-Oct-02 5:45
Daniel Turini18-Oct-02 5:45 
GeneralRe: .NET Server RC1: Weird Pin
Paul Watson18-Oct-02 7:30
sitebuilderPaul Watson18-Oct-02 7:30 
GeneralRe: .NET Server RC1: Weird Pin
Daniel Turini18-Oct-02 7:40
Daniel Turini18-Oct-02 7:40 
GeneralRe: .NET Server RC1: Weird Pin
Topper Price19-Oct-02 11:33
Topper Price19-Oct-02 11:33 
GeneralRe: .NET Server RC1: Weird Pin
Paul Watson20-Oct-02 5:41
sitebuilderPaul Watson20-Oct-02 5:41 
GeneralRe: .NET Server RC1: Weird Pin
leppie20-Oct-02 6:06
leppie20-Oct-02 6:06 
GeneralEscaping nasty HTML tags... Pin
Ray Cassick17-Oct-02 18:50
Ray Cassick17-Oct-02 18:50 
GeneralRe: Escaping nasty HTML tags... Pin
Paul Riley18-Oct-02 0:20
Paul Riley18-Oct-02 0:20 
I tend to allow people to use tags like [b][/b] for bold rather than <b></b>. If they enter HTML-looking tags then it gets converted to use lt/gt tags.

The basic tags I allow are headers, bold, italic, underline. I also allow [link *url*], [mail *url*] and [font *face* *color* *size*] (I keep meaning to improve this last one so that you don't need to enter face and color to change the size but I haven't done it yet).

I also replace line feeds with "<br>", except after a header close and translate ampersand, less than, greater than, pound and quote.

Here's some C# code... it's far from perfect coding (serious lack of comments for one thing and I didn't know about Regex when I wrote this - in fact it was adopted quickly from an old VBScript where RegEx wasn't an option) but it works and you can fiddle it to your needs.
private string Format2Html(string Formatted)
{
	string[] hdrTags = {"h1", "h2", "h3", "h4", "h5"};

	string[] nmlTags = { "b", "i", "u", "center" };

	string[][] splTags = { new string[] { "link", "a", "href=\"%\"" },
				new string[] { "mail", "a", "href=\"mailto:%\"" },
				new string[] { "font", "font", "face=\"%\"", "color=\"%\"", "size=\"%\"" } };

	string rtn = Formatted.Replace("&", "&").
		Replace("<", "<").
		Replace(">", ">").
		Replace("£", "£").
		Replace("\"", """).
		Replace(Environment.NewLine, "<br>" + Environment.NewLine);

	int next = 0, start = 0;

	for (start=rtn.IndexOf('['); start > -1; start=rtn.IndexOf('[', start + 1))
	{
		string replacement = "";
		next = rtn.IndexOf('[', start + 1);
		int end = rtn.IndexOf(']', start + 1);

		if (end == -1) end = rtn.Length - 1;
		if (end > next && next > -1) end = next;

		int length = (end - start) + 1;
		string tag = rtn.Substring(start, length).TrimEnd(']').TrimStart('[').Trim(' ');

		string[] tkn = tag.Split(' ', ',');
		
		for (int i = 0; i < hdrTags.Length; i++)
		{
			if (hdrTags[i] == tkn[0])
			{
				replacement = "<" + hdrTags[i] + ">";
			}
			else if (hdrTags[i] == tkn[0].TrimStart('/'))
			{
				replacement = "</" + hdrTags[i] + ">";
				if (rtn.Substring(end + 1, 4) == "<br>") end += 4;
			}
		}

		for (int i = 0; i < nmlTags.Length; i++)
		{
			if (nmlTags[i] == tkn[0])
			{
				replacement = "<" + nmlTags[i] + ">";
			}
			else if (nmlTags[i] == tkn[0].TrimStart('/'))
			{
				replacement = "</" + nmlTags[i] + ">";
			}
		}

		for (int i = 0; i < splTags.Length; i++)
		{
			if (splTags[i][0] == tkn[0])
			{
				replacement = "<" + splTags[i][1];
				for (int j = 1, k = 2; (j < tkn.Length) && (k < splTags[i].Length); j++, k++)
				{
					while (tkn[j].Length == 0) j++;
					replacement += " " + splTags[i][k].Replace("%", tkn[j]);
				}
				replacement += ">";
			}
			else if (splTags[i][0] == tkn[0].TrimStart('/'))
			{
				replacement = "</" + splTags[i][1] + ">";
			}
		}

		if (replacement.Length > 0)
			rtn = rtn.Remove(start, (end - start) + 1).Insert(start, replacement);
	}

	return rtn;
}
HTH

Paul
Why don't you take a good look at yourself and describe what you see - Led Zeppelin, Misty Mountain Hop
GeneralRe: Escaping nasty HTML tags... Pin
Daniel Turini18-Oct-02 6:11
Daniel Turini18-Oct-02 6:11 
GeneralRe: Escaping nasty HTML tags... Pin
Paul Watson18-Oct-02 7:29
sitebuilderPaul Watson18-Oct-02 7:29 
GeneralRe: Escaping nasty HTML tags... Pin
Ray Cassick20-Oct-02 8:24
Ray Cassick20-Oct-02 8:24 
GeneralRe: Escaping nasty HTML tags... Pin
Richard Deeming21-Oct-02 2:08
mveRichard Deeming21-Oct-02 2:08 
GeneralTwo lines want to others' help. Pin
zhoujun17-Oct-02 15:03
zhoujun17-Oct-02 15:03 
GeneralHome page configuring Pin
Sarvesvara (BVKS) Dasa17-Oct-02 8:47
Sarvesvara (BVKS) Dasa17-Oct-02 8:47 
Questionvb6 webclass equivalent (or better) in asp.net ? Pin
ryancrawcour17-Oct-02 1:45
ryancrawcour17-Oct-02 1:45 
AnswerRe: vb6 webclass equivalent (or better) in asp.net ? Pin
Paul Watson17-Oct-02 22:00
sitebuilderPaul Watson17-Oct-02 22:00 
GeneralRe: vb6 webclass equivalent (or better) in asp.net ? Pin
ryancrawcour18-Oct-02 5:09
ryancrawcour18-Oct-02 5:09 
GeneralRe: vb6 webclass equivalent (or better) in asp.net ? Pin
Paul Watson18-Oct-02 8:22
sitebuilderPaul Watson18-Oct-02 8:22 
GeneralUser Control and Datalist Pin
Mazdak16-Oct-02 22:19
Mazdak16-Oct-02 22:19 
GeneralMeeting trouble when using JSP to connect DB2 Pin
George216-Oct-02 17:02
George216-Oct-02 17:02 
GeneralA custom control and an click event Pin
Paul Watson16-Oct-02 1:38
sitebuilderPaul Watson16-Oct-02 1:38 
GeneralRe: A custom control and an click event Pin
Paul Riley16-Oct-02 2:10
Paul Riley16-Oct-02 2:10 
GeneralRe: A custom control and an click event Pin
Paul Watson16-Oct-02 2:27
sitebuilderPaul Watson16-Oct-02 2:27 
GeneralRe: A custom control and an click event Pin
Paul Riley16-Oct-02 2:33
Paul Riley16-Oct-02 2:33 
GeneralRe: A custom control and an click event Pin
Paul Watson16-Oct-02 2:59
sitebuilderPaul Watson16-Oct-02 2:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.