在进行Discuz二次开发的时候,有时我们需要将编辑器单独使用,由于Discuz发布内容采用了自己独有的bbcode转换方式,为了兼容我们也需要做相应转换。
自己转换不知道规则,只有通过查找源码找到具体的转换函数。
js版本:
引入static/js/bbcode.js
html2bbcode("html内容")//html转bbcode
bbcode2html("bbcode内容")//bbcode转html
php版本:
//html转bbcode
require_once libfile('function/editor');
echo html2bbcode($html_str);
//bbcode转html
require_once libfile('function/discuzcode');
echo discuzcode($bbcode_str, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0);
如下是网友提供的一个完整使用编辑器的例子
<script type="text/javascript" src="{$_G['setting']['jspath']}forum_post.js?{VERHASH}"></script> <script src="static/js/bbcode.js?{VERHASH}" type="text/javascript"></script> <script type="text/JavaScript"> var fid = {$_G['fid']};//插件所在版块ID </script> <!--{subtemplate home/editor_image_menu}--> <textarea class="userData" name="content" id="uchome-ttHtmlEditor" style="height: 100%; width: 100%; display: none; border: 0px"></textarea> <iframe src='home.php?mod=editor&charset={CHARSET}&allowhtml=1&isportal=0' name='uchome-ifrHtmlEditor' id='uchome-ifrHtmlEditor' scrolling='no' style='width:85%;height:400px;border:1px solid #C5C5C5;position:relative;' border=0 frameborder=0 ></iframe> </br> <input type="hidden" name="formhash" id="formhash" value="{FORMHASH}" /> <input type="hidden" name="posttime" id="posttime" value="{$posttime}" /> <input type="hidden" name="wysiwyg" id="e_mode" value="1" /> <input type="hidden" name="special" value="127" /> <input type="hidden" name="specialextra" value="plugin_test" /> <input type="hidden" id="message" name="message" value="" /> <input id='submit_editsubmit' class='btn' type='button' value='提交' name='editsubmit' onClick='validate(this);'> </form> <script type="text/javascript" src="static/image/editor/editor_base.js?{VERHASH}"></script> <script type="text/javascript" src="static/image/editor/editor_function.js?{VERHASH}"></script> <script type="text/javascript"> var textobj = $('uchome-ttHtmlEditor'); var wysiwyg = (BROWSER.ie || BROWSER.firefox || (BROWSER.opera >= 9)) && parseInt('1') == 1 ? 1 : 0; var allowswitcheditor = parseInt('1'); var allowhtml = parseInt('0'); var allowsmilies = parseInt('1'); var allowbbcode = parseInt('1'); var allowimgcode = parseInt('1'); var simplodemode = parseInt('0'); var fontoptions = new Array("宋体", "新宋体", "黑体", "微软雅黑", "Arial", "Verdana", "Mingliu", "Helvetica", "Trebuchet MS", "Tahoma", "Impact", "Times New Roman", "仿宋,仿宋_GB2312", "楷体,楷体_GB2312"); var smcols = 8; var custombbcodes = new Array(); </script> <script type="text/JavaScript">
function validate(obj) {var mcpp = window.frames['uchome-ifrHtmlEditor']; var mcpobj = mcpp.window.frames['HtmlEditor']; edit_save(); jQuery("#message").val(html2bbcode(mcpobj.document.body.innerHTML));//将编辑器内容转bbcode window.onbeforeunload = null; obj.form.submit(); return false; }</script> 在编辑时直接读取数据库中内容无法按html展示,且再次保存时会遇到“您的提交带有不合法参数,谢谢合作”,我们需要预先将存到数据库的bbcode转成html加载到编辑器window.onload = function(){ var mcpp = window.frames['uchome-ifrHtmlEditor']; var mcpobj = mcpp.window.frames['HtmlEditor']; mcpobj.document.body.innerHTML = bbcode2html("{$thread['message']}");//加载完成时将bbcode内容转换成正常的html代码并赋值 }