Turning on BBCODES - Javascript
I'm using this chat http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/ and I tried to turn on the bbcodes but: http://imageshack.us/photo/my-images/827/bbcodes1.png/
I tried to replace in
scripts.js
text : text.replace(/</g,'<').replace(/>/g,'>')
with
text : text.replace(/</g,'<').
开发者_如何学C replace(/>/g,'>').
replace(/\[b\]((\s|.)+?)\[\/b\]/,'<b>\\1</b>')
This is my bbcode function, you can edit the codes variable to support more bbcode. For now, it support only [b], [i], [u], [url], [img].
[read] is my special bbcode for my website
function bbcode(text){
var codes = {};
codes['url'] = {
params : {
href : ''
},
html: "<a href=\"$href\">$text</a>"
};
codes['b'] = {
params : {
},
html : "<b>$text</b>"
};
codes['i'] = {
params : {
},
html : "<i>$text</i>"
};
codes['u'] = {
params : {
},
html : "<u>$text</u>"
};
codes['img'] = {
params : {
},
html : "<img src=\"$text\" />"
};
codes['read'] = {
params : {
},
text : function(text){
for(var x in codes){
text = text.replace(new RegExp('\\[' + x + '\\]','gi'), '').replace(new RegExp('\\[\\/' + x + '\\]','gi'), '');
}
return encodeURIComponent(text);
},
process: function(text){
return text;
},
html : "$text <object type=\"application/x-shockwave-flash\" data=\"" + GLOBALS.baseUrl + "/player_mp3_maxi.swf\" width=\"200\" height=\"20\"><param name=\"movie\" value=\"" + GLOBALS.baseUrl + "/player_mp3_maxi.swf\" /><param name=\"bgcolor\" value=\"#ffffff\" /><param name=\"FlashVars\" value=\"mp3=http://api.ispeech.org/api/rest?apikey%3Ddeveloperdemokeydeveloperdemokey%26action%3Dconvert%26voice%3Dusenglishfemale1%26text%3D$TEXT&width=200&showslider=1\" /></object>"
};
text = text.replace(/\</g, '<').replace(/\>/g, '>');
var nomore = false;
while(!nomore){
var matches = text.match(/\[([^\]]+)\]/gi);
if(matches == null)
return text;
nomore = true;
for(var i = 0; i < matches.length; i++){
var code = matches[i].substring(1, matches[i].length - 1);
var parts = code.split(/\s+/);
if(typeof codes[parts[0]] == 'object'){
//is exist
var obj = {};
//get the params
for(var j = 1; j < parts.length; j++)
{
var temp = parts[j].split('=');
if(typeof codes[parts[0]].params[temp[0]] != 'undefined'){
obj[temp[0]] = (temp.length > 1) ? (temp[1].indexOf('"') == 0 ? temp[1].substring(1, temp[1].length - 1) : temp[1]) : '';
}
}
//find the text
var index = text.indexOf(matches[i]);
var index2 = text.indexOf('[/'+parts[0]+']', index);
if(index2 == -1)
index2 = text.length;
var t = text.substring(index + matches[i].length, index2);
if(typeof codes[parts[0]].process == 'function'){
t = codes[parts[0]].process(t);
}if(typeof codes[parts[0]].text == 'function'){
t2 = codes[parts[0]].text(t);
}
var html = codes[parts[0]].html;
for(var x in obj)
html = html.replace("$" + x, obj[x]);
html = html.replace(/\$text/g, t);
if(typeof codes[parts[0]].text == 'function'){
html = html.replace(/\$TEXT/g, t2);
}
text = text.substr(0, index) + html + text.substr(index2 + parts[0].length + 3);
nomore = false;
}
}
}
text = text.replace(/\n/g, "<br />");
return text;
}
You can use this by:
text: bbcode(text)
精彩评论