PsychicTide 42 Junior Poster

Hey guys, I've been stuck on this problem for quite some time now. I'm trying to have someone type some text into CK editor and then I want to count how many bold, italic, or underlined words have been typed.

I've managed to count the number of tags there are (EI: a single word or group of words that are sorrounded by a bold '<b>' tag)

I can also manage to count the number of words that are in the first tag. if there is only 1 set of styled consecutive word this works (IE: you dont have bold words spread out throughout the text), however, I can't count how many words are in the next index of start and end tags.

below is an example of where I am now. hopefully it's enough to get the idea. Let me know if you need anything else. I've been building from a wordcount plugin

function ShowWordCount(evt) {
var editor = evt.editor;
    if ($('div#cke_wordcount_'+editor.name).length > 0) { // Check element exists
         // Because CKEditor uses Javascript to load parts of the editor, some of its elements are not immediately available in the DOM
        // Therefore, I use setTimeout.  There may be a better way of doing this.
        setTimeout(function() {
            var bcount = 0;
            var icount = 0;
            var ucount = 0;
            var wordCount = GetWordCount(editor.getData());
            // count b, i, and u tags inside ckeditor frame, then count the number of words in each
            var b = (editor.getData().split('<b>').length - 1);
            var i = (editor.getData().split('<i>').length - 1);
            var u = (editor.getData().split('<u>').length - 1);
            var bcount = (bcount + editor.getData().substring(editor.getData().indexOf('<b>'), editor.getData().indexOf('</b>')).split(' ').length - 1);
            var icount = (icount + editor.getData().substring(editor.getData().indexOf('<i>'), editor.getData().indexOf('</i>')).split(' ').length - 1);
            var ucount = (ucount + editor.getData().substring(editor.getData().indexOf('<u>'), editor.getData().indexOf('</u>')).split(' ').length - 1);
            $('div#cke_wordcount_'+editor.name).html('Words Used (Max 50): '+wordCount+' ||b'+bcount+' i'+icount+' u'+ucount+'||'); 

            // Check we are within word limit
            if (wordCount > 50) {
                document.getElementById('cke_wordcount_contents').style.color = 'red'
                editor.execCommand('undo');
            } else if (wordCount == 50)  {
                // Create an undo snapshot as we are on the word limit - next word entered will be undone to return
                // to this snapshot point.
                editor.fire('saveSnapshot');
                document.getElementById('cke_wordcount_contents').style.color = 'red'
            } else {
                document.getElementById('cke_wordcount_contents').style.color = 'black'
            }
        }, 500);
    }
}

Any suggestions are much appreciated.

EDIT: I've tried several different loops similar to indexof('string', (indexof('string') + i)); with no luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.