萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

FCKeditor 整合 PHP 完整實作:初始化、瀏覽器相容模式與動態工具列配置

類別架構與核心流程

<?php
class FCKEditor {
    private $areaname;
    private $rooteditor;
    private $areawidth = '100%';
    private $areaheight = '400px';
    private $bottontype = 'Default';
    private $areavalue = '';
    private $configure = [];
    
    public function load($areaname) {
        $this->initialize($areaname);
        return $this->loadHtml();
    }
    
    private function initialize($areaname) {
        global $urlconf;
        $this->areaname = $areaname;
        $this->rooteditor = $urlconf['domains'] . 'textarea/fck/';
        $this->areavalue = $this->areavalue ?: '';
    }
    
    private function loadHtml() {
        $value = htmlspecialchars($this->areavalue);
        $html = '
'; if ($this->browserSupported()) { // 現代瀏覽器:載入 FCKeditor iframe $file = (isset($_GET['fcksource']) && $_GET['fcksource'] === 'true') ? 'fckeditor.original.html' : 'fckeditor.html'; $link = "{$this->rooteditor}editor/{$file}?InstanceName={$this->areaname}"; if ($this->bottontype) { $link .= "&Toolbar={$this->bottontype}"; } $html .= ''; $html .= ''; $html .= ''; } else { // 降級:傳統 textarea $w = strpos($this->areawidth, '%') === false ? $this->areawidth . 'px' : $this->areawidth; $h = strpos($this->areaheight, '%') === false ? $this->areaheight . 'px' : $this->areaheight; $html .= ""; } $html .= '
'; return $html; } private function browserSupported(): bool { global $HTTP_USER_AGENT; $ua = $HTTP_USER_AGENT ?? ($_SERVER['HTTP_USER_AGENT'] ?? ''); // IE (非 Mac、非 Opera) if (stripos($ua, 'MSIE') !== false && stripos($ua, 'mac') === false && stripos($ua, 'Opera') === false) { $ver = (float)substr($ua, stripos($ua, 'MSIE') + 5, 3); return $ver >= 5.5; } // Gecko if (stripos($ua, 'Gecko/') !== false) { $build = (int)substr($ua, stripos($ua, 'Gecko/') + 6, 8); return $build >= 20030210; } return false; } public function setData($data) { $this->areavalue = stripslashes($data); } } ?>

使用方式

<?php
require_once('FCKEditor.class.php');
$editor = new FCKEditor();
$editor->areawidth = '100%';
$editor->areaheight = '500px';
$editor->bottontype = 'Basic'; // 工具列模式
$editor->setData($articleContent); // 從資料庫載入內容
echo $editor->load('article_content');
?>

關鍵配置說明

屬性說明預設值
rooteditorFCKeditor 安裝根目錄 URL從 $urlconf 取得
areawidth/areaheight編輯區寬高 (支援 px/% )100% / 400px
bottontype工具列配置名稱 (對應 fckconfig.js)Default
fcksourceGET 參數,true 載入原始碼版 (除錯用)false

重要遷移提醒

FCKeditor 已停止維護 (2010 年),後續專案請改用:

  • CKEditor 4/5:官方繼任者,架構現代化、支援 React/Vue/Angular 整合
  • TinyMCE:輕量、模組化、商業授權友善
  • Quill / TipTap / Slate:現代框架優先的富文本編輯器

遷移重點:API 差異大、配置格式不同、輸出 HTML 標準化程度提高。

copyright © 萬盛學電腦網 all rights reserved