Discuz!大师网

搜索
查看: 3698|回复: 0

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

[复制链接]
发表于 2017-5-3 23:39:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
定义文件:\source\function\function_editor.php
函数定义:
  1. function html2bbcode($text) {
  2.         $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>');

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

  6.         $pregfind = array(
  7.                 "/<script.*>.*<\/script>/siU",
  8.                 '/on(mousewheel|mouseover|click|load|onload|submit|focus|blur)="[^"]*"/i',
  9.                 "/(\r\n|\n|\r)/",
  10.                 "/<table([^>]*(width|background|background-color|bgcolor)[^>]*)>/siUe",
  11.                 "/<table.*>/siU",
  12.                 "/<tr.*>/siU",
  13.                 "/<td>/i",
  14.                 "/<td(.+)>/siUe",
  15.                 "/<\/td>/i",
  16.                 "/<\/tr>/i",
  17.                 "/<\/table>/i",
  18.                 '/<h([0-9]+)[^>]*>/siUe',
  19.                 '/<\/h([0-9]+)>/siU',
  20.                 "/<img[^>]+smilieid="(\d+)".*>/esiU",
  21.                 "/<img([^>]*src[^>]*)>/eiU",
  22.                 "/<a\s+?name=.+?".">(.+?)<\/a>/is",
  23.                 "/<br.*>/siU",
  24.                 "/<span\s+?style="float:\s+(left|right);">(.+?)<\/span>/is",
  25.         );
  26.         $pregreplace = array(
  27.                 '',
  28.                 '',
  29.                 '',
  30.                 "tabletag('\\1')",
  31.                 '[hide][table]',
  32.                 '[tr]',
  33.                 '[td]',
  34.                 "tdtag('\\1')",
  35.                 '[/td]',
  36.                 '[/tr]',
  37.                 '[/table][/hide]',
  38.                 ""[size=".(7 - \\1)."]"",
  39.                 "[/size]\n\n",
  40.                 "smileycode('\\1')",
  41.                 "imgtag('\\1')",
  42.                 '\1',
  43.                 "\n",
  44.                 "[float=\\1]\\2[/float]",
  45.         );
  46.         $text = preg_replace($pregfind, $pregreplace, $text);

  47.         $text = recursion('b', $text, 'simpletag', 'b');
  48.         $text = recursion('strong', $text, 'simpletag', 'b');
  49.         $text = recursion('i', $text, 'simpletag', 'i');
  50.         $text = recursion('em', $text, 'simpletag', 'i');
  51.         $text = recursion('u', $text, 'simpletag', 'u');
  52.         $text = recursion('a', $text, 'atag');
  53.         $text = recursion('font', $text, 'fonttag');
  54.         $text = recursion('blockquote', $text, 'simpletag', 'indent');
  55.         $text = recursion('ol', $text, 'listtag');
  56.         $text = recursion('ul', $text, 'listtag');
  57.         $text = recursion('div', $text, 'divtag');
  58.         $text = recursion('span', $text, 'spantag');
  59.         $text = recursion('p', $text, 'ptag');

  60.         $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");
  61.         $pregreplace = array("\n[\\1]", "\\1\n", "\n", '', '', '', '', "\\1");
  62.         $text = preg_replace($pregfind, $pregreplace, $text);

  63.         $strfind = array(' ', '<', '>', '&');
  64.         $strreplace = array(' ', '<', '>', '&');
  65.         $text = str_replace($strfind, $strreplace, $text);

  66.         return dhtmlspecialchars(trim($text));
  67. }
复制代码
使用举例:
  1. require_once libfile('function/editor');
  2. $html='<strong>这里是加粗文本</strong>';
  3. $bbcode = html2bbcode($html);
  4. echo $bbcode;
  5. //输出:[b]这里是加粗文本[/b]
复制代码



回复 马甲回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|网站地图|小黑屋|展会网|Discuz站长论坛 |天天打卡

GMT+8, 2024-4-26 13:18 , Processed in 0.021776 second(s), 4 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表