tinymce php,tinyMCE插件开发之插入html,php,sql,js代码 并代码高亮显示

下面就是我开发的过程。

首先,我的 tinyMCE版本是 Version: 3.2.7 (2009-09-22) 。

下载地址 https://www.jb51.net/codes/17198.html

tinyMCE插入代码,需要调用 tinyMCE的 tinyMCE.execCommand('mceInsertContent',false,value); 方法。其中参数无需改变,value 就是你要插入的内容,

比如我写了一个函数,

function InsertHTML(value)

{

tinyMCE.execCommand('mceInsertContent',false,value);

}

后面,针对该例子,提供下载。在例子中。一共涉及到三个文件。

tinyMCE.html insertcode.php save.php 这三个文件。

tinyMCE.html 是tinyMCE文本框页面。

主要代码如下:

tinyMCE.init({

// General options

convert_urls : false,

mode : "exact",

elements : "Article_Content",

//mode : "textareas",

theme : "advanced",

plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount",

// Theme options

theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",

theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",

theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",

theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",

theme_advanced_toolbar_location : "top",

theme_advanced_toolbar_align : "left",

theme_advanced_statusbar_location : "bottom",

theme_advanced_resizing : true,

// Example content CSS (should be your site CSS)

content_css : "css/content.css",

// Drop lists for link/image/media/template dialogs

template_external_list_url : "lists/template_list.js",

external_link_list_url : "lists/link_list.js",

external_image_list_url : "lists/image_list.js",

media_external_list_url : "lists/media_list.js",

// Replace values for the template plugin

template_replace_values : {

username : "Some User",

staffid : "991234"

}

});

function InsertHTML(value)

{

tinyMCE.execCommand('mceInsertContent',false,value);

}

其中js代码是初始化 tinyMCE。下载的例子中,并未包含 tinyMCE,你需要自己下载。然后 更改js代码的 src 即可。

上面这段代码,是用来打开insertcode.php文件的。

接下来,我们来看下 insertcode。php 这个文件的代码。

首先是 js 代码

function insertcode()

{

var value = $('#postcontent').html();

var codetype = $('#codetype').val();

// window.opener.InsertHTML(''+value+'');

window.opener.InsertHTML('

'+value+'
');

window.close();

}

其次是 PHP 和 html 代码

error_reporting(0);

$content = $_POST['content'];

if(!empty($content))

{

$codetype = $_POST['codetype'];

echo '

';

$content = htmlspecialchars($content);

echo $content;

echo '

';

}else

{

?>

选择要插入的代码类型

php

js

html

c

asp

xml

java

java

C#

SQL

}

?>

在insertcode.php中,insertcode() 函数用来调用 tinyMCE.html页面的 insertHTMl()函数,并将代码插入到 tinyMCE.html 页面中。

代码中,我们为什么要 '+value+' 呢?

因为我们在显示页面,将会采用 SyntaxHighlighter 插件来高亮显示代码。

还有一点要说明,在这里,$content = htmlspecialchars($content); 我们对于代码本身,进行了 htmlspecialchars 转义操作。这样,插入数据库的代码则会是安全的。

OK,我们再来看 save.php,该页面用来显示 提交的内容。

主要代码如下:

$Article_Content = $_POST['Article_Content'];

function transcode($str)

{

if(empty($str))

{

return false;

}

$str = str_replace('"','"',$str);

$str = str_replace('','',$str);

$str = str_ireplace('<BR>',"n",$str);

$str = str_ireplace('

 
 

return $str;

}

echo transcode($Article_Content);

?>

dp.SyntaxHighlighter.HighlightAll('code');

OK,完了。

^_^ ~~~

tinyMCE 插件开发之插代码高亮 v1.0 (支持html,php,sql,js)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值