Discuz开发之HTML转Discuz代码(bbcode)函数html2bbcode()

定义文件:\source\function\function_editor.php
函数定义:

function html2bbcode($text) {
$text = strip_tags($text, '<table><tr><td><b><strong><i><em><u><a><div><span><p><strike><blockquote><ol><ul><li><font><img><br><br/><h1><h2><h3><h4><h5><h6><script>');


if(ismozilla()) {
$text = preg_replace("/(?<!<br>|<br \/>|\r)(\r\n|\n|\r)/", ' ', $text);
}


$pregfind = array(
"/<script.*>.*<\/script>/siU",
'/on(mousewheel|mouseover|click|load|onload|submit|focus|blur)="[^"]*"/i',
"/(\r\n|\n|\r)/",
"/<table([^>]*(width|background|background-color|bgcolor)[^>]*)>/siUe",
"/<table.*>/siU",
"/<tr.*>/siU",
"/<td>/i",
"/<td(.+)>/siUe",
"/<\/td>/i",
"/<\/tr>/i",
"/<\/table>/i",
'/<h([0-9]+)[^>]*>/siUe',
'/<\/h([0-9]+)>/siU',
"/<img[^>]+smilieid=\"(\d+)\".*>/esiU",
"/<img([^>]*src[^>]*)>/eiU",
"/<a\s+?name=.+?\".\">(.+?)<\/a>/is",
"/<br.*>/siU",
"/<span\s+?style=\"float:\s+(left|right);\">(.+?)<\/span>/is",
);
$pregreplace = array(
'',
'',
'',
"tabletag('\\1')",
'[table]',
'[tr]',
'[td]',
"tdtag('\\1')",
'[/td]',
'[/tr]',
'[/table]',
"\"[size=\".(7 - \\1).\"]\"",
"[/size]\n\n",
"smileycode('\\1')",
"imgtag('\\1')",
'\1',
"\n",
"[float=\\1]\\2[/float]",
);
$text = preg_replace($pregfind, $pregreplace, $text);


$text = recursion('b', $text, 'simpletag', 'b');
$text = recursion('strong', $text, 'simpletag', 'b');
$text = recursion('i', $text, 'simpletag', 'i');
$text = recursion('em', $text, 'simpletag', 'i');
$text = recursion('u', $text, 'simpletag', 'u');
$text = recursion('a', $text, 'atag');
$text = recursion('font', $text, 'fonttag');
$text = recursion('blockquote', $text, 'simpletag', 'indent');
$text = recursion('ol', $text, 'listtag');
$text = recursion('ul', $text, 'listtag');
$text = recursion('div', $text, 'divtag');
$text = recursion('span', $text, 'spantag');
$text = recursion('p', $text, 'ptag');


$pregfind = array("/(?<!\r|\n|^)\[(\/list|list|\*)\]/", "/<li>(.*)((?=<li>)|<\/li>)/iU", "/<p.*>/iU", "/<p><\/p>/i", "/(<a>|<\/a>|<\/li>)/is", "/<\/?(A|LI|FONT|DIV|SPAN)>/siU", "/\[url[^\]]*\]\[\/url\]/i", "/\[url=javascript:[^\]]*\](.+?)\[\/url\]/is");
$pregreplace = array("\n[\\1]", "\\1\n", "\n", '', '', '', '', "\\1");
$text = preg_replace($pregfind, $pregreplace, $text);


$strfind = array(' ', '<', '>', '&');
$strreplace = array(' ', '<', '>', '&');
$text = str_replace($strfind, $strreplace, $text);


return dhtmlspecialchars(trim($text));
}

使用举例:

require_once libfile('function/editor');
$html='<strong>这里是加粗文本</strong>';
$bbcode = html2bbcode($html);
echo $bbcode;
//输出:[b]这里是加粗文本[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春哥一号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值