php處理ckeditor分頁符問題
ckeditor有個分頁的按鈕,能夠插入分頁符,但這隻是在編輯時顯示的效果而已,要真正實現分頁,還需要其它語言,這裡使用php採取一種方法來實現分頁,當然還有其它的方法可以實現。
這裡使用的方法是:在顯示的頁面讀取資料後,根據ckeditor插入的分頁代碼將內容分成幾部分存放在資料中,ckeditor源碼中插入的分頁代碼是:
style=”page-break-after: always;”> style=”display: none;”>
在Firefox中插入的代碼也是如此,但是如果是在ie中編輯,則插入的代碼是:
style=”page-break-after: always”> style=”display: none”>
因此,在將內容轉為數組時,使用Regex進行匹配以防止不同瀏覽器儲存的內容不一致。匹配的Regex如下:
“/\s* <\/span>\s*<\/div>/”
我在測試時,與之間被添加分行符號,所以用了“\s*”進行匹配,在後邊的與之間也用了“ \s*”進行匹配以防萬一。將此功能寫成函數,如下:
/** * 擷取文章內容(當前分頁)
* * @param string $content 文章內容
* @param integer $page 頁數
* @return array
*/
function get_article_content($content, $page=1){
$page = $page ? intval($page) :
$article = array( ’info’ => array(), ’pages’ => 1 );
if(!emptyempty($content)){
$pattern = ”/\s* <\/span>\s*<\/div>/”; $contents = preg_split($pattern, $content);
$article['pages'] = count($contents);
($page > $article['pages']) && $page = $article['pages'];
$article['info'] = $contents[$page - 1];
}
return $article;
}