使用 JavaScript/jQuery 将 BBcode 转换为 HTML
Convert BBcode to HTML using JavaScript/jQuery
我可以得到一些帮助来覆盖一些PHP代码到jQuery/JavaScript吗?我想要的是一个简单的BBCode到HTML转换器。
这是PHP代码。我想使用 jQuery/JavaScript 实现同样的事情。
$str = htmlentities($str);
// The array of regex patterns to look for
$format_search = array(
'#'[b'](.*?)'[/b']#is',
'#'[i'](.*?)'[/i']#is',
'#'[u'](.*?)'[/u']#is',
);
// The matching array of strings to replace matches with
$format_replace = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<span style="text-decoration: underline;">$1</span>',
);
// Perform the actual conversion
$str = preg_replace($format_search, $format_replace, $str);
感谢您的帮助!
看起来您只需要将#
更改为/
,is
更改为ig
但我还必须将/b
更改为'/b
现场演示
$str = 'this is a [b]bolded[/b] and [i]italic[/i] string';
// The array of regex patterns to look for
$format_search = [
/'[b'](.*?)'['/b']/ig,
/'[i'](.*?)'['/i']/ig,
/'[u'](.*?)'['/u']/ig
]; // note: NO comma after the last entry
// The matching array of strings to replace matches with
$format_replace = [
'<strong>$1</strong>',
'<em>$1</em>',
'<span style="text-decoration: underline;">$1</span>'
];
// Perform the actual conversion
for (var i =0;i<$format_search.length;i++) {
$str = $str.replace($format_search[i], $format_replace[i]);
}
alert($str)
其他现场演示
function boldFunc(str, p1, offset, s) {
return '<strong>'+encodeURIComponent(p1)+'</strong>'
}
function italicFunc(str, p1, offset, s) {
return '<em>'+encodeURIComponent(p1)+'</em>'
}
function underlinedFunc(str, p1, offset, s) {
return '<span class="un">'+encodeURIComponent(p1)+'</span>'
}
$str = 'this is a [b]bölded[/b], [i]itälic[/i] and [u]ünderlined[/u] [i]strïng[/i]';
// The array of regex patterns to look for
$format_search = [
/'[b'](.*?)'['/b']/ig,
/'[i'](.*?)'['/i']/ig,
/'[u'](.*?)'['/u']/ig
]; // NOTE: No comma after the last entry
// The matching array of strings to replace matches with
$format_replace = [
boldFunc,
italicFunc,
underlinedFunc
];
// Perform the actual conversion
for (var i =0;i<$format_search.length;i++) {
$str = $str.replace($format_search[i], $format_replace[i]);
}
document.getElementById('output').innerHTML=$str;
相关文章:
- Html画布自由转换
- 强制转换 HTML 对象元素
- js/jquery/other library插件来转换HTML->XML
- 如何转换 HTML DOM 结构
- 如何在javascript中正确转换html
- 转换HTML画布内容,在laravel后端向其发送角度形式的数据请求
- 使用JavaScript(RegExp)转换HTML实体
- 函数无法转换html
- wkhtmltopdf转换html到pdf得到JavaScript执行超时异常
- 转换html页面表示为文本到dom对象
- 使用客户端JavaScript来转换HTML页面而不是从服务器发送多个HTML页面是明智的吗?
- 需要转换HTML表格在CSS与提交表单
- 转换HTML 5画布图纸格式可读的3D打印机
- 任何javascript内置函数转换HTML实体从url传递
- 如何在文档中转换HTML元素
- 转换HTML文本框完全只读
- 垂直分页转换:HTML, CSS, Javascript
- Meteor:我如何转换HTML/HTML5代码来使用Meteor
- 用于转换 HTML 代码的模板
- 点击按钮转换html页面图像和下载使用JS,Jquery