BBCodes for .net?

G

Guest

Hello, I want to allow my users to type in some codes, like the BBCodes. The
codes i had in mind were:

[bold]Message[/bold]
[italic]Message[/italic]
[strikethru]Message[/strikethru]


Message


Message


Message

[zitat=username]Message[/zitat] <-- here i need the username to be
transferd into a variable so i can fetch the corresponding post and then
replaced by the post returned form the DB
[internallink=private/adress.aspx?ID=5]Rules[/internallink] <-- program
should automatically add <a href ... /zone/ and then the url and then the
message </a>
[threadlink=687]Message[/threadlink] <-- program should automatically add
<a href ... zone/board/answer.aspx?TopicID= and then the number supplied and
then the message </a>
Message
(e-mail address removed)

Whats the best way to achive this?
Also, i want to make sure that the end tag is after the opening tag,
otherwise it could be pretty bad, like:

Message <-- No end link

or

[link=[URL="http://www.goole.com]Message"]www.goole.com]Message[/URL] <-- End link before link

these would case the rest of my site to become a link. (using c#)

Patrick
 
G

Guest

Howdy Patrick,

Use regular expressions. Please find a simple example i prepared to show you
the concept

-- begin c# code --

public static string ReplaceZizats(string input)
{
// one from many possible patterns,
// @"\[zitat=(\w+)](.+)\[/zitat]"
string pattern = @"\[zitat=([^\]]*)\]([^\[]*)\[/zitat\]";

return System.Text.RegularExpressions.Regex.Replace(input, pattern,
new MatchEvaluator(ZizatMatchEvaluator));
}

private static string ZizatMatchEvaluator(
System.Text.RegularExpressions.Match match)
{
if (match.Success)
{
string username = match.Groups[1];
string message = match.Groups[2];

// get the post from database using above variables
// ...

return String.Format("<a href=\"postspage.aspx?postId={0}\">{1}</a>",
postId, message);
}
else
{
return match.Value;
}
}
-- end c# code --

Milosz
 
G

Guest

Tiny bug:
string username = match.Groups[1];
string message = match.Groups[2];

Should be:

string username = match.Groups[1].Value;
string message = match.Groups[2].Value;

--
Milosz


Milosz Skalecki said:
Howdy Patrick,

Use regular expressions. Please find a simple example i prepared to show you
the concept

-- begin c# code --

public static string ReplaceZizats(string input)
{
// one from many possible patterns,
// @"\[zitat=(\w+)](.+)\[/zitat]"
string pattern = @"\[zitat=([^\]]*)\]([^\[]*)\[/zitat\]";

return System.Text.RegularExpressions.Regex.Replace(input, pattern,
new MatchEvaluator(ZizatMatchEvaluator));
}

private static string ZizatMatchEvaluator(
System.Text.RegularExpressions.Match match)
{
if (match.Success)
{
string username = match.Groups[1];
string message = match.Groups[2];

// get the post from database using above variables
// ...

return String.Format("<a href=\"postspage.aspx?postId={0}\">{1}</a>",
postId, message);
}
else
{
return match.Value;
}
}
-- end c# code --

Milosz

Patrick F said:
Hello, I want to allow my users to type in some codes, like the BBCodes. The
codes i had in mind were:

[bold]Message[/bold]
[italic]Message[/italic]
[strikethru]Message[/strikethru]


Message


Message


Message

[zitat=username]Message[/zitat] <-- here i need the username to be
transferd into a variable so i can fetch the corresponding post and then
replaced by the post returned form the DB
[internallink=private/adress.aspx?ID=5]Rules[/internallink] <-- program
should automatically add <a href ... /zone/ and then the url and then the
message </a>
[threadlink=687]Message[/threadlink] <-- program should automatically add
<a href ... zone/board/answer.aspx?TopicID= and then the number supplied and
then the message </a>
Message
(e-mail address removed)

Whats the best way to achive this?
Also, i want to make sure that the end tag is after the opening tag,
otherwise it could be pretty bad, like:

Message <-- No end link

or

[link=[URL="http://www.goole.com]Message"]www.goole.com]Message[/URL] <-- End link before link

these would case the rest of my site to become a link. (using c#)

Patrick
 

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