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

I am using JavaScript as well as PHP to calculate string lengths. Everything is OK except when I calcul

I am using JavaScript as well as PHP to calculate string lengths. Everything is OK except when I calculate a textarea string length ing 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 the 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.

I am using JavaScript as well as PHP to calculate string lengths. Everything is OK except when I calculate a textarea string length ing 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 the 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.

Share Improve this question edited May 23, 2017 at 12:19 CommunityBot 11 silver badge asked Oct 4, 2011 at 0:52 user977191user977191 8712 gold badges9 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743964541a4537294.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信