编辑器 ubb转换 js函数
最近需要整合layedit到discuz插件,每次提交都会出现内容非法提示,兼容解决方式就是将文本内容进行ubb然后存库。ubb转换js如下:
function htmlToUbb(str){
//str = str.replace(/(\r\n|\n|\r)/ig, '');
str = str.replace(/<br[^>]*>/ig,'\n');
str = str.replace(/<p[^>\/]*\/>/ig,'\n');
//str = str.replace(/\(.+?)\[\/code\]/ig, function($1, $2) {return phpcode($2);});
str = str.replace(/\son[\w]{3,16}\s?=\s*([\'\"]).+?\1/ig,'');
str = str.replace(/<hr[^>]*>/ig,'');
str = str.replace(/<(sub|sup|u|strike|b|i|pre)>/ig,'[$1]');
str = str.replace(/<\/(sub|sup|u|strike|b|i|pre)>/ig,'[/$1]');
str = str.replace(/<(\/)?strong>/ig,'[$1b]');
str = str.replace(/<(\/)?em>/ig,'[$1i]');
str = str.replace(/<(\/)?blockquote([^>]*)>/ig,'[$1blockquote]');
str = str.replace(/<img[^>]*smile=\"(\d+)\"[^>]*>/ig,'');
str = str.replace(/<img[^>]*src=[\'\"\s]*([^\s\'\"]+)[^>]*>/ig,''+'$1'+'');
str = str.replace(/<a[^>]*href=[\'\"\s]*([^\s\'\"]*)[^>]*>(.+?)<\/a>/ig,''+'$2'+'');
//str = str.replace(/<h(+)([^>]*)>(.*?)<\/h\1>/ig,function($1,$2,$3,$4){return h($3,$4,$2);});
str = str.replace(/<[^>]*?>/ig, '');
str = str.replace(/&/ig, '&');
str = str.replace(/</ig, '<');
str = str.replace(/>/ig, '>');
return str;
}
function ubbToHtml(str){
str = str.replace(/</ig,'<');
str = str.replace(/>/ig,'>');
str = str.replace(/\n/ig,'<br />');
//str = str.replace(/\(.+?)\[\/code\]/ig, function($1, $2) {return phpCode($2);});
str = str.replace(/\/ig,'<hr />');
str = str.replace(/\[\/(size|color|font|backcolor)\]/ig,'</font>');
str = str.replace(/\[(sub|sup|u|i|strike|b|blockquote|li)\]/ig,'<$1>');
str = str.replace(/\[\/(sub|sup|u|i|strike|b|blockquote|li)\]/ig,'</$1>');
str = str.replace(/\[\/align\]/ig,'</p>');
str = str.replace(/\[(\/)?h()\]/ig,'<$1h$2>');
str = str.replace(/\/ig,'<p align="$1">');
str = str.replace(/\/ig,'<font size="$1">');
str = str.replace(/\+?)\]/ig, '<font color="$1">');
str = str.replace(/\+?)\]/ig, '<font style="background-color:$1">');
str = str.replace(/\+?)\]/ig, '<font face="$1">');
str = str.replace(/\(.+?)\[\/list\]/ig,'<ol type="$1">$2</ol>');
str = str.replace(/\[(\/)?list\]/ig,'<$1ul>');
//str = str.replace(/\/ig,function($1,$2){ return smilePath($2);});
str = str.replace(/\([^\[]*)\[\/img\]/ig,'<img src="$1" border="0" />');
str = str.replace(/\]+)\]([^\[]+)\[\/url\]/ig, '<a href="$1">'+'$2'+'</a>');
str = str.replace(/\([^\[]+)\[\/url\]/ig, '<a href="$1">'+'$1'+'</a>');
return str;
}
**** Hidden Message *****
页:
[1]