Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript String Length Differs From PHP mb_strlen

Tags:

People also ask

Do strings have a length property JavaScript?

The length of a string in JavaScript can be found using the . length property. Since . length is a property it must be called through an instance of a string class.

What is the length of string in JavaScript?

JavaScript does not return the length but rather returns the code units occupied by the string. It uses the UTF-16 string formatting methods to store characters. This essentially means that the characters in your string are encoded into a 16-bit long binary number before being stored.

How long is a string in PHP?

Strings are always 2GB as the length is always 32bits and a bit is wasted because it uses int rather than uint.

Is there a length function in JavaScript?

The length function in Javascript is used to return the length of an object. And since length is a property of an object it can be used on both arrays and strings. Although the syntax of the length function remains the same, bear in mind that the interpretation of length varies between arrays and strings.


I use document.getElementById("text").value.length to get the string length through javascript, and mb_strlen($_POST['text']) to get the string length by PHP and both differs very much. Carriage returns are converted in javascript before getting the string length, but I guess some characters are not being counted.

For example,

[b]15. Umieszczanie obrazka z logo na stronie zespołu[/b]

This block of text is calculated 57 in javascript and 58 in PHP. When the text gets long, the difference increases. Is there any way to overcome this?