理論的には、これは非常に単純です。文字列の長さを「制限」する関数を作成し、長すぎる場合は削除するだけです。
<?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 );
編集:配列に文字を追加するときに文字数を「制限」したい場合は、関数でチェックを作成するだけです:
<?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;
}