找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2801|回复: 0

[分享] Discuz!开发之HTML转Discuz代码(bbcode)函数html2bbcode()

[复制链接]
发表于 2017-11-14 13:53:14 | 显示全部楼层 |阅读模式 来自 河南省新乡市
定义文件:\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.   
  4.   
  5.     if(ismozilla()) {  
  6.         $text = preg_replace("/(?<!<br>|<br \/>|\r)(\r\n|\n|\r)/", ' ', $text);  
  7.     }  
  8.   
  9.   
  10.     $pregfind = array(  
  11.         "/<script.*>.*<\/script>/siU",  
  12.         '/on(mousewheel|mouseover|click|load|onload|submit|focus|blur)="[^"]*"/i',  
  13.         "/(\r\n|\n|\r)/",  
  14.         "/<table([^>]*(width|background|background-color|bgcolor)[^>]*)>/siUe",  
  15.         "/<table.*>/siU",  
  16.         "/<tr.*>/siU",  
  17.         "/<td>/i",  
  18.         "/<td(.+)>/siUe",  
  19.         "/<\/td>/i",  
  20.         "/<\/tr>/i",  
  21.         "/<\/table>/i",  
  22.         '/<h([0-9]+)[^>]*>/siUe',  
  23.         '/<\/h([0-9]+)>/siU',  
  24.         "/<img[^>]+smilieid="(\d+)".*>/esiU",  
  25.         "/<img([^>]*src[^>]*)>/eiU",  
  26.         "/<a\s+?name=.+?".">(.+?)<\/a>/is",  
  27.         "/<br.*>/siU",  
  28.         "/<span\s+?style="float:\s+(left|right);">(.+?)<\/span>/is",  
  29.     );  
  30.     $pregreplace = array(  
  31.         '',  
  32.         '',  
  33.         '',  
  34.         "tabletag('\\1')",  
  35.         '[table]',  
  36.         '[tr]',  
  37.         '[td]',  
  38.         "tdtag('\\1')",  
  39.         '[/td]',  
  40.         '[/tr]',  
  41.         '[/table]',  
  42.         ""[size=".(7 - \\1)."]"",  
  43.         "[/size]\n\n",  
  44.         "smileycode('\\1')",  
  45.         "imgtag('\\1')",  
  46.         '\1',  
  47.         "\n",  
  48.         "[float=\\1]\\2[/float]",  
  49.     );  
  50.     $text = preg_replace($pregfind, $pregreplace, $text);  
  51.   
  52.   
  53.     $text = recursion('b', $text, 'simpletag', 'b');  
  54.     $text = recursion('strong', $text, 'simpletag', 'b');  
  55.     $text = recursion('i', $text, 'simpletag', 'i');  
  56.     $text = recursion('em', $text, 'simpletag', 'i');  
  57.     $text = recursion('u', $text, 'simpletag', 'u');  
  58.     $text = recursion('a', $text, 'atag');  
  59.     $text = recursion('font', $text, 'fonttag');  
  60.     $text = recursion('blockquote', $text, 'simpletag', 'indent');  
  61.     $text = recursion('ol', $text, 'listtag');  
  62.     $text = recursion('ul', $text, 'listtag');  
  63.     $text = recursion('div', $text, 'divtag');  
  64.     $text = recursion('span', $text, 'spantag');  
  65.     $text = recursion('p', $text, 'ptag');  
  66.   
  67.   
  68.     $pregfind = array("/(?<!\r|\n|^)
  69. (\/list|list|\*)
  70. /", "/<li>(.*)((?=<li>)|<\/li>)/iU", "/<p.*>/iU", "/<p><\/p>/i", "/(<a>|<\/a>|<\/li>)/is", "/<\/?(A|LI|FONT|DIV|SPAN)>/siU", "/
  71. url[^
  72. ]*\]
  73. \/url
  74. /i", "/
  75. url=javascript:[^
  76. ]*\](.+?)
  77. \/url
  78. /is");  
  79.     $pregreplace = array("\n[\\1]", "\\1\n", "\n", '', '', '', '', "\\1");  
  80.     $text = preg_replace($pregfind, $pregreplace, $text);  
  81.   
  82.   
  83.     $strfind = array(' ', '<', '>', '&');  
  84.     $strreplace = array(' ', '<', '>', '&');  
  85.     $text = str_replace($strfind, $strreplace, $text);  
  86.   
  87.   
  88.     return dhtmlspecialchars(trim($text));  
  89. }  
复制代码
使用举例:
  1. require_once libfile('function/editor');  
  2. $html='<strong>这里是加粗文本</strong>';  
  3. $bbcode = html2bbcode($html);  
  4. echo $bbcode;  
  5. //输出:[b]这里是加粗文本[/b]  
复制代码


发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

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

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-4-24 22:13 , Processed in 0.038963 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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