2011-09-08 96 views

回答

0

这是我的bbcode函数,你可以编辑co des变量来支持更多的bbcode。目前,它仅支持[b],[i],[u],[url],[img]。
[阅读]是我的特殊设置高亮我的网站

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&amp;width=200&amp;showslider=1\" /></object>" 
    }; 
    text = text.replace(/\</g, '&lt;').replace(/\>/g, '&gt;'); 
    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; 
} 

您可以使用此:

text: bbcode(text) 
+0

我会努力的!谢谢 ! –

+0

我试过你的代码,例如[b]的东西[/ b]它的工作,但几秒后返回没有效果。 :( –

+0

也许你需要编辑你的php代码,我猜这个脚本有一个函数来重写'some second'后面的聊天消息,并且客户端更新来自服务器 - >效果的新消息已被磨损 –