$str="dsa3d123as1d32as5d465SADASDASDas2d13as2514d3asDASDASd";
$arr='';
for($i=0;$i<4;$i++)
{
$arr.=$str[rand(0,strlen($str)-1)];
}
echo $arr;
需要注意的是如果使用拼接為一個變量并且最后輸出的話,$arr必須定義為字符串而不能定義為數組,然后使用.=字符串操作符來進行追加。否則,使用echo會拋出數組到字符串的錯誤。
$arr='';
for($i=0;$i<4;$i++)
{
$arr.=$str[rand(0,strlen($str)-1)];
}
echo $arr;
需要注意的是如果使用拼接為一個變量并且最后輸出的話,$arr必須定義為字符串而不能定義為數組,然后使用.=字符串操作符來進行追加。否則,使用echo會拋出數組到字符串的錯誤。
2017-08-25
function yam($length){
$a="1234567890";
$b="qwertyuiopasdfghjklzxcvbnm";
$c="QWERTYUIOPASDFGHJKLZXCVBNM";
$d=$a.$b.$c;
for ($e=0;$e<$length;$e++){
//echo $d{mt_rand(1, strlen($d)-1)};
$f{$e}=$d{mt_rand(1, strlen($d)-1)};
}
return $f;
}
//yam(4);
$g=yam(4);
foreach ($g as $key=>$x){
echo $x;
}
$a="1234567890";
$b="qwertyuiopasdfghjklzxcvbnm";
$c="QWERTYUIOPASDFGHJKLZXCVBNM";
$d=$a.$b.$c;
for ($e=0;$e<$length;$e++){
//echo $d{mt_rand(1, strlen($d)-1)};
$f{$e}=$d{mt_rand(1, strlen($d)-1)};
}
return $f;
}
//yam(4);
$g=yam(4);
foreach ($g as $key=>$x){
echo $x;
}
2017-07-28
function getCode($length = 4) {
$varcode = "0123456789qwertyuiopasdfgjklzxcbnmvQWERTYUIOPASDLKFJHGZBXNCMV";
$relult = "";
for ($i = 0; $i < $length; $i++) {
$relult{$i} = $varcode{mt_rand(0,strlen($varcode))};
}
return $relult;
}
echo getCode();
$varcode = "0123456789qwertyuiopasdfgjklzxcbnmvQWERTYUIOPASDLKFJHGZBXNCMV";
$relult = "";
for ($i = 0; $i < $length; $i++) {
$relult{$i} = $varcode{mt_rand(0,strlen($varcode))};
}
return $relult;
}
echo getCode();
2017-07-11
$str2{1} = '' 輸出 報錯 Warning: Cannot assign an empty string to a string offset in E:\www\myself\string\index2.php on line 35
2017-06-13