天天看點

Ueditor更改統計字數與内容儲存去除字元實體

Ueditor 預設統計方式為統計字元,今天項目中要求輸入英文寫作,是以要對單詞數進行統計。直接上代碼

<script type="text/javascript">  
	//初始化Ueditor
    var ue = UE.getEditor('container', {  
    toolbars: [  
    ['undo', 'redo', 'bold', 'italic', 'underline','rowspacingtop','rowspacingbottom','lineheight','fontfamily','fontsize','justifyleft','justifycenter','justifyright','justifyjustify','fullscreen']  
    ],  
    autoHeightEnabled: false,  
    autoFloatEnabled: true,
    elementPathEnabled:false,
    wordCount:false,//*這是重點 關閉自帶的字數統計
    });
    //當Ueditor加載完成後,顯示統計字元的div并更改内容
    ue.addListener( 'ready', function( editor ) {
	    //edui1_wordcount div為字元統計的顯示區域,關閉字元統計後 display=none 是以要顯示
    	$("#edui1_wordcount").show();
    	$("#edui1_wordcount").html("單詞統計");
    } );
    //為Ueditor添加鍵盤監聽事件,每次敲擊後統計單詞數
    ue.addListener('keydown',function()count(ue.getContentTxt())});
    /* 單詞統計 */
    function count(str){
        var value=str;
        //标點轉換為空格
        value=value.replace(/[\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g," "); 
        //統計中文個數
        var chinese=value.match(/[\u4e00-\u9fa5]/g);
        //去除中文
        value = value.replace(/[\u4e00-\u9fa5]+/g, " ");
        // 将換行符,前後空格替換為空格
        value = value.replace(/\n|\r|^\s+|\s+$/gi," ");
        // 多個空格替換成一個空格
        value = value.replace(/\s+/gi," ");
        // 更新計數
        var length = 0;
        var match = value.match(/\s/g);
        if (match) {
            length = match.length + 1;
        } else if (value) {
            length = 1;
        }
        length = length+(chinese?chinese.length:0);
        $("#edui1_wordcount").html("您總共輸入了"+length+"個單詞");
    }
    /* 單詞統計 */
</script> 
           

統計單詞部分寫的不夠完善,請自行忽略

下面是java對Ueditor富文本的處理

主要兩部分:

1.去除html标簽,百度上一大堆。

2.處理html中的字元實體例如:

Ueditor更改統計字數與内容儲存去除字元實體

可以使用

org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(html);實體名稱轉為html标簽(“& l t;”→“<”)

org.apache.commons.lang3.StringEscapeUtils.escapeHtml4(html);html标簽轉為實體名稱(“<”→“& l t;”)

繼續閱讀