从CKedit或pastefromword中删除不必要的标记


Remove unnecessary tags from CKEditor pastefromword

我想分享我是如何在CKEditor中清理pastefromword中的数据的。

只需包含我自己答案中的代码并使用

保存或查看文本区域内容时,php中的clean_tags()。up

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: jpietal
 * Date: 22.02.13
 * Time: 11:35
 * To change this template use File | Settings | File Templates.
 */
/* @return string
 * @author Milian <mail@mili.de>
 */
function closetags($html) {
//#put all opened tags into an array
preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
$openedtags = $result[1];   //#put all closed tags into an array
preg_match_all('#</([a-z]+)>#iU', $html, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
//#all tags are closed
if (count($closedtags) == $len_opened) {
    return $html;
}
$openedtags = array_reverse($openedtags);
//#close tags
for ($i=0; $i < $len_opened; $i++) {
    if (!in_array($openedtags[$i], $closedtags)){
        $html .= '</'.$openedtags[$i].'>';
    } else {
        unset($closedtags[array_search($openedtags[$i], $closedtags)]);    
    }
}
return $html;
}
/* @return string
 * @author Jacek Pietal
 */
function cleanup_tags($string) {
$replaceSpaces = str_replace('&nbsp;', ' ', $string);
$removeEvilTags = strip_tags($replaceSpaces, '<a><div><br><ul><li><b><i><u><em><strong>');
$closeEvilTags = closetags($removeEvilTags);
$replaceSlashes = str_replace('''''', '''', $closeEvilTags);
$stripSlashes = stripslashes($replaceSlashes);
return $stripSlashes;
}
?>