Reputation: 409
In my php page I have textarea input box where I want to limit the characters up to 1000 only.
For that, first I am using HTML maxlength and then valid the length using PHP mb_strlen
too but both PHP and HTML counting is not equal.
For example:
Here is my text:
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of 'de Finibus Bonorum et Malorum' (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, 'Lorem ipsum dolor sit amet..', comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from 'de Finibus Bonorum et Malorum' by Cicero are also reproduced in their exact original form, accompaniedd
Now, In that HTML textarea box, I just copy and past that text and then press the submit button to validate the text using PHP.
But PHP is showing me 1002 characters? Why? The HTML maxlength should be limit the text up to 1000 characters so my PHP code should not be showing the 1002 characters error message.
PHP code:
$data = rtrim($data);
echo mb_strlen($data); // 1002 characters
Is there any mistake? How can I solve it?
Thank You.
Updated:
$data = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of 'de Finibus Bonorum et Malorum' (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, 'Lorem ipsum dolor sit amet..', comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from 'de Finibus Bonorum et Malorum' by Cicero are also reproduced in their exact original form, accompaniedd";
$data = trim($data);
echo mb_strlen($data);
?>
<textarea maxlength="1000" cols="100" rows="20"></textarea>
and Output is 1002 characters!
Finally, Can you tell me what is best solution to limit the textarea length?
Upvotes: 2
Views: 680
Reputation: 3399
I know this is an old question, but it was the first result in Google. See this answer about line breaks \n in html/JavaScript and \r\n in PHP.
Also to clarify, you need mb_strlen
because certain characters, like emoji, will count as multiple characters in PHP.
Here is the solution:
echo strlen( $text ); // 5132 (incorrect due to emoji)
echo mb_strlen( $text ); // 2566 (incorrect due to linebreaks)
$text = preg_replace( "/\r\n|\r/", "\n", $text );
echo mb_strlen($order_notes); // 2500 (correct)
Upvotes: 0
Reputation: 36
$data = trim($data);
use strlen($data);
Better you use jquery for 1000 chars restriction. It will not reload whole page.
Upvotes: 0