PDA

View Full Version : Replace code using looping and database


CurtWRC
04-28-2006, 11:18 AM
Currently I'm using about 15 different replace codes to change BBc codes to HTML tags. However this means I have to copy & paste the same code for every message input on my site to keep it consistent like so:

message.Text = Replace(message.Text, vbCrLf, "<br />")
message.Text = Replace(message.Text, "[b]", "<b>")
message.Text = Replace(message.Text, "[u]", "<u>")
message.Text = Replace(message.Text, "[i]", "<i>")

However Ive heard its possible to loop the replace code using a database. So I created a table in my database and put the BBc codes in the 'Before' field and the HTML tags in the 'After' table. However I don't have a clue how to loop this. I imagine I would put something like this in the loop:

message.text = Replace(message.Text, "Before Field", "After Field")

If anyone can help that would be much appreciated, Thanks.

jak0b
05-04-2006, 11:10 PM
i think that loop and string replace in db will very charge your server

i sugest first use System.Text.StringBuilder to replace strings
so:
Hashtable bbcodes = new Hashtable();
form db or from some file insert to hashtable your bb codes, as key use bb code i.e. [b] , as value use html code i.e. <strong>

StringBuilder sb = new StringBuilder( your_msg );
foreach( string key in bbcodes.Keys )
{
sb.replace( key, bbcodes[key].ToString() );
}

if you have more complicate bb cone like [color=red] you can use regular expresion or insert on your list bb code to html code all supported combinations

if u still wnat to do this in db read about cursors (t-sql), but create cursor and use it is very cost

CurtWRC
05-11-2006, 12:47 PM
I don't fully understand what you mean. Is that a loop?