开发者

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

开发者 https://www.devze.com 2023-04-11 01:24 出处:网络
I am using JavaScript as well as PHP to calculate string lengths. Everything is OK except when I calculate a textarea string length coming from Chrome or Firefox versus Internet Explorer. New textarea

I am using JavaScript as well as PHP to calculate string lengths. Everything is OK except when I calculate a textarea string length coming from Chrome or Firefox versus Internet Explorer. New textarea rows/linebreaks are calculated as 2 chars in Internet Explorer and PHP. But Chrome and Firefox calculate textarea rows/linebreaks just as 1 char (versus PHP and Internet Explorer calculates as 2). This is my problem. Anyone knows how to resolve it?

So JavaScript char count in a textarea in IE = the string length calculated with PHP /// And JavaScript char count in a textarea in Chrome/Firefox does not equal the string length calculated with PHP.

Code used:

Javascript  :  document.getElementById("text").value.length

PHP  :  mb_strlen($_POST['text'], 'UTF-8')

There are other posts about this, mainly th开发者_如何学运维e following one: Javascript String Length Differs From PHP mb_strlen . But the answer to my question is not there and couldn't find it on any other posts.

Thanks for your help and suggestions.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号