从CKEditor pastefromword中删除不必要的标签

时间:2013-02-22 10:59:52

标签: php tags ckeditor

我想分享一下如何在CKEditor中清除pastefromword中的数据。

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

保存或查看textarea content.up

时,php中的

clean_tags()

1 个答案:

答案 0 :(得分:0)

<?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;
}

?>