Differences between character counting in PHP and Javascript

I’ve recently codded my own validation rules for forms. I’m checking length of a string on a client side and also on a server side (obviously it’s a good practice). After my script went on a production, users started to complain about validation errors (even if data in a form was passed proper). This is really strange but…

PHP and Javascript are counting chars differently! If something went passed  through JS rules it went wrong on a server side. This happens when there are new line chars in a text.  PHP is counting every new line as one char and JS as two. That’s because PHP interprets line break as \n and JS as \r\n. Let’s look at comparison:

Litwo! Ojczyzno moja! Ty jesteś jak zdrowie. Ile cię trzeba cenić, ten zamek dziś nagodzi do gospody. Słudzy nie bywa.

Zresztą zdać się z jego pamięć droga co zaledwie dotykał się rówiennicą a u niego ze szkoły: więc o muzyce.

Prostym ludziom wokanda zda się małą a w naukach mniej zgorszenia. Ach, ja rozumiem! Wy Polaki, ja wam służyć, moje.

 

FunctionLanguageLength
strlenPHP345
mb_strlenPHP371
str.lengthJavascript349

Remedy for the problem

You can use mb_strlen equivalent for counting chars in JS:

function mb_strlen(str) {
    var len = 0;
    for(var i = 0; i < str.length; i++) {
        len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? 2 : 1;
    }
    return len;
}

In server side you can replace \r\n to \n:

str_replace( "\r\n","\n", $attribute );

Remember that:

And last but not least, always use mb_strlen if you can! It’s safer and properly counts chars in different encodings.



Posted

in

,