php将字符串随机分割成不同长度数组的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php将字符串随机分割成不同长度数组的方法。分享给大家供大家参考。具体分析如下:

这里使用php对字符串在指定的长度范围内进行随机分割,把分割后的结果存在数组里面

function RandomSplit($min, $max, $str){
  $a = array();
  while ($str != ''){
    $p = rand($min, $max);
    $p = ($p > strlen($str)) ? strlen($str) : $p;
    $buffer = substr($str, 0, $p);
    $str = substr($str, $p, strlen($str)-$p);
    $a[] = $buffer;
  }
  return $a;
}
//范例:
/*
** Example:
*/
$test_string = 'This is a example to test the RandomSplit function.';
print_r(RandomSplit(1, 7, $test_string));
/*
Outputs something like this
(Array items are 1 to 7 characters long): 
Array
(
  [0] => This
  [1] => is
  [2] => a exam
  [3] => ple to
  [4] => test t
  [5] => he
  [6] => 
  [7] => ran
  [8] => d_spl
  [9] => it f
  [10] => un
  [11] => ction.
)
*/

希望本文所述对大家的php程序设计有所帮助。

相关文章

php之CodeIgniter学习笔记

在使用数据库之前,我们最好将数据库进行自动连接:config/autoload.php自动加载 $autoload['libraries'] = array('database');一些...

win10 apache配置虚拟主机后localhost无法使用的解决方法

win10 apache配置虚拟主机后localhost无法使用的解决方法

win10系统配置虚拟主机 1.用记事本或Sublime Text打开httpd.conf ctrl + f 搜索httpd-vhosts.conf 将 #Include conf...

php上传图片类及用法示例

本文实例讲述了php上传图片类及用法。分享给大家供大家参考,具体如下: 1.类文件名为:upclass.php <?php class upclass{ public...

探讨Hessian在PHP中的使用分析

探讨Hessian在PHP中的使用分析

什么是Hessian  Hessian是由caucho提供的一种开源的远程通讯协议。  采用二进制 RPC 协议,基于 HTTP 传输,服务器端不用另开放防火墙端口。  协议的规范是公开...

PHP定时任务延缓执行的实现

复制代码 代码如下: /* |--------------------------- |PHP定时任务 |@黑眼诗人 <www.chenwei.ws> |----------...