php - Characters per line and lines per textarea limit - Stack Overflow

I'm trying to create a multiline textarea in a php page, and I want to validate so that if the use

I'm trying to create a multiline textarea in a php page, and I want to validate so that if the user can't insert more than 50 characters per line, or more than 50 lines. The idea is that the user can paste something from a spreadsheet, but if one line has more than 50 characters, the rest will be discarded, and not inserted in the next line. That's why I want to avoid the idea of having 50 individual textboxes.

It would be ideal if this could be done in javascript (or php itself, but I didn't saw anything like it in php).

Thanks!

UPDATE: Thanks for all that answers, but that would work only after the user submitted the form, right? For example, if the max lines is 3 instead of 50, and the user inserts 100 consecutive characters, and then a line break, it would only have 50 characters left, limiting the inputs to 2 instead of 3. Hope I was clear...

I'm trying to create a multiline textarea in a php page, and I want to validate so that if the user can't insert more than 50 characters per line, or more than 50 lines. The idea is that the user can paste something from a spreadsheet, but if one line has more than 50 characters, the rest will be discarded, and not inserted in the next line. That's why I want to avoid the idea of having 50 individual textboxes.

It would be ideal if this could be done in javascript (or php itself, but I didn't saw anything like it in php).

Thanks!

UPDATE: Thanks for all that answers, but that would work only after the user submitted the form, right? For example, if the max lines is 3 instead of 50, and the user inserts 100 consecutive characters, and then a line break, it would only have 50 characters left, limiting the inputs to 2 instead of 3. Hope I was clear...

Share Improve this question edited Aug 31, 2009 at 14:01 nightshadex101 asked Aug 31, 2009 at 13:39 nightshadex101nightshadex101 531 silver badge6 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

Here it is, based on the code provided by Matthew, but this is shorter and also limits the number of lines.

$lines = array_slice(explode("\n", $string), 0, 50); // max 50 lines

foreach ($lines as $key => $value)
{
    $lines[$key] = substr(trim($value), 0, 50); // max 50 chars
}

$string = implode("\n", $lines);

PHP:

$max_length = 50;
$lines = explode("\n", $input);
for($i = 0; $i < count($lines); $i++)
{
    $lines[$i] = rtrim($lines[$i]); // just incase there's a "\r"
    $lines[$i] = (strlen($lines[$i]) <= 50 ? 
                      $lines[$i] : 
                      substr($lines[$i], 0, 50));
}
$input = implode("\n", $lines);

You could break this down into two processes:

1: Use jQuery (or what ever JavaScript library you like) to validation the 50 character line limit for each line. The validation plugin offers a lot of functionality that might be useful.

2: Use php to double check just in case the use disables the JavaScript on the browser side. This would have to be the on submit functionality due to JavaScript being disabled (So no Ajax)

So what you would have to do is read the text area into an array using the line break \n to split the text into each element in the array. Now check each array element for the 50 character limitation. And now that you have all the text into an array you can also check the rows as well by array size.

Use explode("\n", $str) to break the textarea data into lines, then check the length of each line.

A similar method could be used on the user end with Javascript, but if you want to ensure security you must do it on the PHP end because Javascript can be circumvented.

Ultimately it depends on whether this is to make the user's life easier or to prevent bad things happening.

Find length of longest line:

$length = array_reduce(array_map(explode("\n", $string), 'strlen'), 'max', 0);

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

相关推荐

  • php - Characters per line and lines per textarea limit - Stack Overflow

    I'm trying to create a multiline textarea in a php page, and I want to validate so that if the use

    4小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信