0

I'm using the CKEDITOR to write a description and a summary gets auto added. The summary has a character count to stop at 300 characters. The following is a test line I wrote hitting return to take a new line.

This is a line and
a new line doesn't count
the same as php

This results in JS = 59 chars and PHP = 61 chars. The new line is getting parsed differently somehow. This leads to a serverside error when chacking if the summary is more than 300 chars because the JS says 300 but PHP is getting maybe 302

CKEDITOR.replace('description');
CKEDITOR.instances['description'].on('key', function() {

        var html = CKEDITOR.instances['description'].getData();
        var value = $('<div/>', { html: html }).text();

        if(value.length > 300){
            var text = value.substring(0, 297);
            $('#summary').html(text + '...');
        }   else {
            $('#summary').html(value);
        }

        char_count(limit);
});
4
  • 1
    Just remove newlines before you check the string length, problem solved ? Commented Jul 16, 2014 at 10:01
  • Needs to be parsed so when retireved from db it will appear as user entered it. Commented Jul 16, 2014 at 10:03
  • Great, so that's what you save. For validating see @adeneos comment. Commented Jul 16, 2014 at 10:04
  • How you parsed it is irrelevant to how you check the length ? Commented Jul 16, 2014 at 10:04

1 Answer 1

2

This is due to newline characters. On Windows, newlines are presented as CRLF (two characters) whereas on *nix, newlines are represented as LF (1 character).

One possible way of resolving this issue would be to strip the CR characters from the PHP string. They are unnecessary and take up a (negligible) amount of space:

$str = str_replace("\r", '', $str);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.