zoukankan      html  css  js  c++  java
  • Discuz!开发之HTML转Discuz代码(bbcode)函数html2bbcode()

    定义文件:sourcefunctionfunction_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 />|
    )(
    |
    |
    )/", ' ', $text);
    	}
    
    
    	$pregfind = array(
    		"/<script.*>.*</script>/siU",
    		'/on(mousewheel|mouseover|click|load|onload|submit|focus|blur)="[^"]*"/i',
    		"/(
    |
    |
    )/",
    		"/<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",
    		"/<as+?name=.+?".">(.+?)</a>/is",
    		"/<br.*>/siU",
    		"/<spans+?style="float:s+(left|right);">(.+?)</span>/is",
    	);
    	$pregreplace = array(
    		'',
    		'',
    		'',
    		"tabletag('\1')",
    		'[table]',
    		'[tr]',
    		'[td]',
    		"tdtag('\1')",
    		'[/td]',
    		'[/tr]',
    		'[/table]',
    		""[size=".(7 - \1)."]"",
    		"[/size]
    
    ",
    		"smileycode('\1')",
    		"imgtag('\1')",
    		'1',
    		"
    ",
    		"[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("/(?<!
    |
    |^)[(/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("
    [\1]", "\1
    ", "
    ", '', '', '', '', "\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]
    

      

  • 相关阅读:
    极高效内存池实现 (cpu-cache)
    gles2.0环境的在windows上的建立
    使用OpenGL绘制 shapefile文件 完成最基本的gis操作
    纯C++安卓开发 (ndk)系列之 ---- 常见问题
    如何用 纯C++(ndk)开发安卓应用 ?
    Android-NDK处理用户交互事件
    图解-安卓中调用OpenGL
    图解安卓-c++开发-通过java 调用c++ jni的使用
    搭建安卓开发环境 hello world andriod
    关于socket通讯,如何才能高效?
  • 原文地址:https://www.cnblogs.com/chenjian/p/7688861.html
Copyright © 2011-2022 走看看