PHP 随机取一字符串中的一部分字符

来源:百度知道 编辑:UC知道 时间:2024/06/04 08:10:38
$str=65,87,16,78,54,32,91,56

随便取这里的一个数 请高手们帮忙
对了 还有就是随机选出来的数,比如78 如何拆成$x=7和$y=8

PHP随机取字符串中一部分字符示例代码:

<?php
//字符串
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";

//1.获取字符串的长度
$length = strlen($str)-1;

//2.字符串截取开始位置
$start=rand(0,$length);

//3.字符串截取长度
$count=rand(0,$length);

//4.随机截取字符串,取其中的一部分字符串
$data=substr($str, $start,$count);

echo $data;
?>

<head>
<title>Demo</title>
</head>
<body>
<?
$num = array(65,87,16,78,54,32,91,56);
for($i = 0; $i < 100; ++$i)
{
$s = $num[rand(0, 7)];
$x = $s / 10;
$y = $s % 10;
printf("%d %d %d<br>", $s, $x, $y);
}
?>
</body>
</html>