我正在尝试创建一个随机字符串,它应该恰好包含30个字符。但有时它也会创建28个字符。如何解决这个问题?
我的代码如下:
function random_string(){
$character_set_array = array( );
$character_set_array[ ] = array( 'count' => 15, 'characters' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ' );
$character_set_array[ ] = array( 'count' => 6, 'characters' => '0123456789' );
$character_set_array[ ] = array( 'count' => 9, 'characters' => '!@#$%^&+_|~-=\{}[]:?/' );
$temp_array = array( );
foreach ( $character_set_array as $character_set )
{
for ( $i = 0; $i < $character_set[ 'count' ]; $i++ )
{
$temp_array[ ] = $character_set[ 'characters' ][ rand( 0, strlen( $character_set[ 'characters' ] ) - 1 ) ];
}
}
shuffle( $temp_array );
return implode( '', $temp_array );
}
请帮助我。