Jump to content

Restrict array size / length ??


doa24uk

Recommended Posts

Hi guys,

 

I've got a textarea input .... I'm aware that I can't count / restrict the lines only the characters ... plus I don't want to use JS (needs to be foolproof / JS un-enabled proof).

 

The problem is that someone can submit as many lines as they wish.

 

I want to set the max array size (array values) to say 50...

 

I presume I'll have to count() the array and then somehow dump the 51st, 52nd, n... values in the array?

Link to comment
https://forums.phpfreaks.com/topic/195841-restrict-array-size-length/
Share on other sites

Solved ....

 

For those looking for the same thing.... array_slice

 

 

// Get Textarea string
$textarea = $_POST['phrase'];

// Explode textarea on newlines to get individual search phrases
$textarea_array = explode("\n", $textarea);


// Slice off the unwanted items
// First value should be 0 (start of array)
// Second value should be your upper limit (10 in my case)
// Anything about the upper limit will be removed

$ResultArray = array_slice($textarea_array, 0, 10);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.