Hello,

I am using counter to count characters in text area. Now the problem, if the number of characters calculated = 160 characters, then when I send the paragraph to my mobile phone, I get 162 characters, I got the reason, but can't find solution yet. Example: write normal paragraph in the text area without clicking on "enter button",

NOTE: when you are typing the paragraph, if the current word faced the end of the line, the whole word will be dropped to second line and so on, please see red star indicator.

The text will be organized itself inside the textarea, but at each line if the word dropped down, I noticed 2 spaces will be added to the previous row. Please look below.

the text(160 characters) in TEXTAREA (rows:8, columns:62) will be shown as below in green:

Hello how are you? Hope great, I am very good and hope to see*
your car in the near future. Let me hear your updates and*
perhaps we can meet very soon in your.tc

The paragraph will be received on my mobile as below(please notice the extra space after "see" and after "and" words):
-----------------
Hello how are you? Hope great, I am very good and hope to see your car in the near future. Let me hear your updates and perhaps we can meet very soon in your.tc
-----------------
To make it clearer:::: sea please the [2spaces] indicator.
Hello how are you? Hope great, I am very good and hope to see[2spaces]your car in the near future. Let me hear your updates and[2spaces]perhaps we can meet very soon in your.tc


Your advice will be appreciated.

Thanks.

Recommended Answers

All 16 Replies

then when I send the paragraph to my mobile phone, I get 162 characters

see if you have a javascript onsubmit function that changes carriage returns and/or new lines to spaces before submitting.

Also, check (for the same thing mentioned above) on the server script to which you are posting/sending the information to.

see if you have a javascript onsubmit function that changes carriage returns and/or new lines to spaces before submitting.

Also, check (for the same thing mentioned above) on the server script to which you are posting/sending the information to.

Thanks but I need to remove that new line comes for the text area, not to replace it. I need a function (PHP or Javascipt) that removes these lines which come automatically from the text area!! Any advice? Thanks a lot.

I solved the problem. Can Daniweb give me credit? :P

I used PHP:

$text = str_replace("\r\n","\n",$text);

$text_array = explode(' ',$text);
foreach($text_array as $ta)
$nobreak [] = trim($ca);
$text = implode(' ',$nobreak);

you completely missed my point. I was trying to get you to see if you already had code that was already replacing \n with ' ' (double spaces) in which case you should have changed that to a single space or perhaps an empty string.

you completely missed my point. I was trying to get you to see if you already had code that was already replacing \n with ' ' (double spaces) in which case you should have changed that to a single space or perhaps an empty string.

Do you know how to change it to empty string? You mean: $text . = ''; ?

Thanks.

Let me clarify. Typically in Unix/Linux systems, when you press the Enter key it produces \n character. In Macs, you get \r, while in Windows you get \r\n (TWO characters even though you pressed one key - this is likely your problem). I was trying to get you to revised your code to see if you are already changing \r and/or \n to single space, in which case it explains why you were ending up with back to back spaces.

You can simply replace \r\n to a single \n. If you are actually insterested in NOT having newlines at all and are infact converting them to spaces you can just do: $text = preg_replace('(\r\n|\r|\n)',' ',$text); If you do not want the newlines changed to spaces then use: $text = preg_replace('(\r\n|\r|\n)','',$text);

Let me clarify. Typically in Unix/Linux systems, when you press the Enter key it produces \n character. In Macs, you get \r, while in Windows you get \r\n (TWO characters even though you pressed one key - this is likely your problem). I was trying to get you to revised your code to see if you are already changing \r and/or \n to single space, in which case it explains why you were ending up with back to back spaces.

You can simply replace \r\n to a single \n. If you are actually insterested in NOT having newlines at all and are infact converting them to spaces you can just do: $text = preg_replace('(\r\n|\r|\n)',' ',$text); If you do not want the newlines changed to spaces then use: $text = preg_replace('(\r\n|\r|\n)','',$text);

Thanks for your reply, but you know, still there is problem.

When you write on textarea without pressing enter, then word which reaches the final limit of the textarea width will fall down right? Now the textarea will add space before the word that fallen down that you did not enter and this will result in extra space in the text.

Now the textarea will add space before the word that fallen down that you did not enter and this will result in extra space in the text.

That should not be happening. At least the browser will NOT add any space to your textarea. Visually, the browser should handle the "wrapping" for you, but would NOT add an actual space character to the user input.

Even if that were the case(not due to the browser but by some client-side scripting) you can just look for sequences of back to back spaces and replace them: $text = preg_replace('\s\s+',' ',preg_replace('(\r\n|\r|\n)',' ',$text));

That should not be happening. At least the browser will NOT add any space to your textarea. Visually, the browser should handle the "wrapping" for you, but would NOT add an actual space character to the user input.

Even if that were the case(not due to the browser but by some client-side scripting) you can just look for sequences of back to back spaces and replace them: $text = preg_replace('\s\s+',' ',preg_replace('(\r\n|\r|\n)',' ',$text));

preg_replace last one is not working in PHP man. It gave empty result.

The problem is that when the word fall down by reaching width limit of text area, new line will be added which should not happen!! How can we solve this problem without trimming spaces?

what's the symbol of the new line added at the limit width of the textarea when the word falls down? "\r"? "\r\n"? or "\n"? Any idea to remove it?

Like I said before, the browser will NOT "tamper" with the ACTUAL user input when doing some word wrapping. Meaning it will NOT insert any "symbol" to force the text onto the next line. If anything, it is possible you may have some javascript (perhaps a library or plugin) doing so, in which case you need to revise your own script.

Like I said before, the browser will NOT "tamper" with the ACTUAL user input when doing some word wrapping. Meaning it will NOT insert any "symbol" to force the text onto the next line. If anything, it is possible you may have some javascript (perhaps a library or plugin) doing so, in which case you need to revise your own script.

Thanks but how to prevent from that new line when the word dropped down without pressing enter? No one answered this yet!

Like I said, look at your scripts (plugins and/or library) to see where this might be happening. Inserting a new line in your actual input is NOT something the browser does.

Can you please write me the script? I am not getting you1

No, man :)
You have white spaces, \r, \r\n, \n before and after your dynamic value because you have to write (for Example)

<textarea><?php echo $somestuff->description ? $somestuff->description: ''; ?></textarea>

instead of

<textarea>
   <?php echo $categoria->descrizione ? $categoria->descrizione: ''; ?>
</textarea>

All you write between open and close tags of textareas is considered. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.