JavaScript String Length Differs from PHP Mb_Strlen

Javascript String Length Differs From PHP mb_strlen (on textarea row, line breaks)

Firefox and Chrome use only \n for linebreaks while IE(opera too) uses \r\n

To get unique results replace all occurrences of \r\n by \n before counting the string-length

mb_strlen() counts new lines twice - difference to .length() in JavaScript

I think PHP is counting a newline as \n. The easy way is to count the number of occurrences in the string and subtract if from the length:

$newLines = substr_count($text, "\n");

$length = mb_strlen($elements['#value']) - $newLines;

Check length by characters like mb_strlen in JS

If you're using UTF-8, I guess that the following function will work for you,

as it should replace all multiple-byte characters and then get the length:

function countChars(str) {  return str.replace(/[\u0080-\u10FFFF]/g, "x").length;}
console.log(countChars('abc abc abc'));

Why PHP strlen() and Javascript xxx.length is not equal?

It's because you are using not the normal dash within following lines 4.8–8 tonnes (11,000–18,000 lb). This character uses 3 bytes (you used it twice, so it's 6 instead of 2 length).

To prevent that you can use mb_strlen($string) or change the , with an -.

I would recommend using the mb_ variant, so you are not only safe for the future, but also don't remove possible Typo`s (if this "dash" is actually the correct dash.. there are so many https://typefacts.com will help you a lot if this is in your interest).

mb_strlen() & strlen() don't return correct values from an Ajax call to PHP

PHP is a byte processor, it is not charset-aware. That has a number of tricky consequences.

Strlen() returns the length in bytes, not the length in characters. This is because php's "string" type is actually an array of bytes. Utf8 uses more than one byte per character for the 'special characters'. Therefore strlen() will only give you the right answer for a narrow subset of text (= plain english text).

Mb_strlen() treats the string as actual characters, but assumes it's in the encoding specified via mbstring.internal_encoding, because the string itself is just an array of bytes and does not have metadata specifying its character set. If you are working with utf8 data and set internal_encoding to utf8 it will give you the right answer. If your data is not utf8 it will give you the wrong answer.

Mysql will receive a stream of bytes from php, and will parse it based on the database session's character set, which you set via the SET NAMES directive. Everytime you connect to the database you must inform it what encoding your php strings are in.

The browser receives a stream of bytes from php, and will parse it based on the content-type charset http header, which you control via php.ini default_charset. The ajax call will submit in the same encoding as the page it runs from.

Summarized, you can find advice on the following page on how to ensure all your data is treated as utf8. Follow it and your problem should resolve itself.
http://malevolent.com/weblog/archive/2007/03/12/unicode-utf8-php-mysql/

Trying to add a character limit to a text input but javascript and php show different string lengths

You may be dealing with multicharacter UTF-8 chars. To have those counted correctly, assuming your files are actually UTF-8, try this:

mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8"); // Not actually needed in this case, but add it if you use preg_ functions
echo mb_strlen("¦");

mb_* functions are meant to deal with multibyte characters.



Related Topics



Leave a reply



Submit