侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

php  /  管理员 发布于 7年前   305

下面就是我开发的过程。
首先,我的 tinyMCE版本是 Version: 3.2.7 (2009-09-22) 。
下载地址 ///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文本框页面。
主要代码如下:
复制代码 代码如下:
<script type="text/javascript" src="https:///tinymce/tiny_mce.js"></script>
<script type="text/javascript">
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"
}
});
</script>
<script type="text/javascript">
function InsertHTML(value)
{
tinyMCE.execCommand('mceInsertContent',false,value);
}
</script>

其中js代码是初始化 tinyMCE。下载的例子中,并未包含 tinyMCE,你需要自己下载。然后 更改js代码的 src 即可。
复制代码 代码如下:
<input name="button" type="button" onclick="window.open('insertcode.php','插入代码','height=500, width=600, top=300, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')" value="点击这里插入代码" />

上面这段代码,是用来打开insertcode.php文件的。
接下来,我们来看下 insertcode。php 这个文件的代码。
首先是 js 代码
复制代码 代码如下:
<script language="javascript" src="http://www.gosoa.com.cn/js/jquery.js"></script>
<script language="javascript">
function insertcode()
{
var value = $('#postcontent').html();
var codetype = $('#codetype').val();
// window.opener.InsertHTML('<textarea rows="3" cols="50" name="code" class="'+codetype+'">'+value+'</textarea>');
window.opener.InsertHTML('<pre name="code" class="'+codetype+'">'+value+'</pre>');
window.close();
}
</script>

其次是 PHP 和 html 代码
复制代码 代码如下:
<?php
error_reporting(0);
$content = $_POST['content'];
if(!empty($content))
{
    $codetype = $_POST['codetype'];
    echo '<div id="postcontent">';
    $content = htmlspecialchars($content);
    echo $content;
    echo '</div>
    <input type="hidden" name="codetype" id="codetype" value="'.$codetype.'" />
    <input type="button" name="Submit" value="提交" onclick="insertcode()" style="border:1px solid #000; line-height:18px; width:60px;"/>';
}else
{
?>
<div style="margin:0 auto">
<form id="form1" name="form1" method="post" action="insertcode.php">
<label>选择要插入的代码类型
<select name="codetype" id="codetype">
    <option value='php'>php</option>
    <option value='js'>js</option>
    <option value='html'>html</option>
    <option value='c'>c</option>
    <option value='asp'>asp</option>
    <option value='xml'>xml</option>
    <option value='java'>java</option>
    <option value='java'>java</option>
    <option value='CSharp'>C#</option>
    <option value='sql'>SQL</option>
</select>
</label>
<label>
<textarea name="content"cols="30" rows="20" style="width:600px; height:200px; border:1px dashed #333"></textarea>
</label>
<p>
<label style="padding-left:50px;">
<input type="Submit" name="Submit" value="提交" style="border:1px solid #000; line-height:18px; width:60px;"/>
</label>
</p>
<p> </p>
</form>
</div>
<?php
}    
?>

在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('<pre','<pre name="code" ',$str);
return $str;
}
echo transcode($Article_Content);
?>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shCore.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushCSharp.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushPhp.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushJScript.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushJava.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushVb.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushSql.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushXml.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushDelphi.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushPython.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushRuby.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushCss.js"></script>
<script class="javascript" src="https:tinymce/lightcode/Scripts/shBrushCpp.js"></script>
<script class="javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>

OK,完了。
^_^ ~~~
tinyMCE 插件开发之插代码高亮 v1.0 (支持html,php,sql,js)


  • 上一条:
    php下cms程序转移服务器的一般步骤
    下一条:
    shell脚本一键安装php7的实例(推荐)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客