0

I have an array with multiple strings, like this:

$str[0] = "kasdnkjsandjabsdkjsbad";
$str[1] = "kasdnkjsandjasdjksabdjkasbkjdsak";
$str[2] = "kasdnkjsandsadbaskjdbsakjdbasdsadjsadjksabdk";
$str[3] = "kasdnkjsandasdjbaskdbjsakbdkjsabdjksabdkjsabdjkasbdjksa";
$str[4] = "kasdnkjsandsajdbaskjdbjksabdjkbasjkdbjksadbjksadbjksadbjksadbkjsa";
$str[5] = "kasdnkjsandasdjbsakdjbsakjdbsakjdsakjdbjksabdjksabdkjasbkjdasbkjdbsakjdsabjk";

Is there any way i can limit the string length of all arrays, assuming i don't know how many strings there will be in a certain array.

Thanks,

3
  • What do you mean with "limit the string length"? Commented Apr 23, 2012 at 12:14
  • Also, have you tried anything yourself? Commented Apr 23, 2012 at 12:14
  • You will have to walk (loop, foreach, etc) the array and slice all the strings up to your length limit - is that what you mean? Check out substr func: php.net/manual/en/function.substr.php Commented Apr 23, 2012 at 12:15

3 Answers 3

3

Well, theoretically, this is pretty simple: just create a function that will "limit" the length of the string, and if it's too long, strip it off:

<?php

$str = array( );
$str[0] = "kasdnkjsandjabsdkjsbad";
$str[1] = "kasdnkjsandjasdjksabdjkasbkjdsak";
$str[2] = "kasdnkjsandsadbaskjdbsakjdbasdsadjsadjksabdk";
$str[3] = "kasdnkjsandasdjbaskdbjsakbdkjsabdjksabdkjsabdjkasbdjksa";
$str[4] = "kasdnkjsandsajdbaskjdbjksabdjkbasjkdbjksadbjksadbjksadbjksadbkjsa";
$str[5] = "kasdnkjsandasdjbsakdjbsakjdbsakjdsakjdbjksabdjksabdkjasbkjdasbkjdbsakjdsabjk";

$length = 10;

$substr = function( $element ) use ( $length ) {
    return substr( $element, 0, $length );
};

$str = array_map( $substr, $str ); 

var_dump( $str );

EDIT: if you want to "limit" the number of characters while adding them to the array, you can simply create a check in a function:

<?php

function append( &$array, $value, $length = 25 ) {
    if( mb_strlen( $value ) > $length ) {
        return false;
    }
    return $array[] = $value;
}

$strings = array( );

append( $strings, 'kasdnkjsandjabsdkjsbad' ); // is accepted.
append( $strings, 'kasdnkjsandjabsdkjsbadkasdnkjs' ); // is not accepted
append( $strings, 'kasdnkjsandsadbaskjdbsakjdbasdsadjsadjksabdk' ); // is not accepted.



// if you *really* want to exit when a value is too long:
function append_exit( $array, $value, $length = 25 ) {
    if( mb_strlen( $value ) > $length ) {
        trigger_error( 'Value is too long.', E_USER_ERROR );
        exit;
    }
    return $array[] = $value;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Since the strings are words, is there any way to exit(); if the string is too long (this is just a spam checker)
0

I am assuming you want to iterate through each string in the array and trim it to a specific length.

Create a function that does this to a single string (as an argument). Use array_map to apply this function to each element in the array.

Try this yourself - if you have any problems, append your attempt to your question.

Comments

0

you could use

substr( $myString, $startInt, [$endInt] );

to limit each string to only so many characters. An Example:

for( $i = 0; $i < sizeof( $myArray ); $i++ ){
    $myArray[$i] = substr( $myString, 0, 20 );
}

That will limit the strings to 20 characters in length, if I did that right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.