Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linefeed , carriage return should take 2 characters #14

Open
agasoftadmin opened this issue Oct 7, 2018 · 7 comments
Open

linefeed , carriage return should take 2 characters #14

agasoftadmin opened this issue Oct 7, 2018 · 7 comments

Comments

@agasoftadmin
Copy link

agasoftadmin commented Oct 7, 2018

hi
\n and \r should be taking 2 characters, please correct that
on the javascript and teh php version
help on this would be much appriciated

@danxexe
Copy link
Owner

danxexe commented Oct 8, 2018

I'm not sure what the issue is. \n and \r are part of the GSM 7-bit encoding and should only use byte each for strings detected as GSM_7BIT. If you are seeing \n or \r take up 2 bytes it's either because:

  • The message you are testing has characters not supported by GSM_7BIT or GSM_7BIT_EX, which the library should correctly detect as UTF16.
  • Your SMS provider is always using UTF16 encoding, even for cases where the string is encodable in GSM_7BIT. In this case you might not need this library since every character will be encoded using 2 bytes anyway.

Do you have an example message where the length is incorrectly detected?

@agasoftadmin
Copy link
Author

agasoftadmin commented Oct 9, 2018 via email

@agasoftadmin
Copy link
Author

agasoftadmin commented Oct 9, 2018

the php script that you have suggested is counting find, 2 chrs for every line break
but the javascript is taking it as one only.

echo SMSCounter::count("This is the first line
Second line started with a return
Third line started with shift enter, now just filling up all the words to make it exactly 160 characte.")->messages;

this show 2 msg count in php, but only 1 msg count via javascript

image

@danxexe
Copy link
Owner

danxexe commented Oct 9, 2018

OK, I see what the problem is.

The lib is correctly counting both \n and \r characters, you can try it yourself by opening your browser's console in a page that has the library included and running SmsCounter.count("\n\r"). The result should be {encoding: "GSM_7BIT", length: 2, per_message: 160, remaining: 158, messages: 1}.

The thing is, the content of an HTML textarea will use just \n for line breaks when using the js DOM API, but will actually use \r\n when submitting the form. That's what you are seeing on the PHP side.

My suggestion is to strip \r characters on the PHP side. It should work with just a \n for most providers.

@agasoftadmin
Copy link
Author

i found out a way to match the php to the javascript
added this line
length += (text.match(/\n/g)||[]).length;
thanks for the help

@itnirmaan
Copy link

itnirmaan commented Aug 27, 2019

You can change \n\r to just \n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants