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 XML error parsing SOAP payload on line 1

WebService,想必大家都比较熟悉,是由“服务提供方”向“服务调用方”提供服务的一种方式。里面有几项关键的技术: XML:描述数据的标准方法 SOAP:简单对象访问协议,用于信息交...

PHP实现自动识别Restful API的返回内容类型

如题,PHP如何自动识别第三方Restful API的内容,自动渲染成 json、xml、html、serialize、csv、php等数据? 其实这也不难,因为Rest API也是基于...

php 无限级分类学习参考之对ecshop无限级分类的解析 带详细注释

复制代码 代码如下:function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset...

PHP时间戳和日期相互转换操作实例小结

本文实例总结了PHP时间戳和日期相互转换操作。分享给大家供大家参考,具体如下: 在php中我们要把时间戳转换日期可以直接使用date函数来实现,如果要把日期转换成时间戳可以使用strto...

file_get_contents("php://input", "r")实例介绍

file_get_contents("php://input", "r")实例介绍

解释不清,直接上例子index.html复制代码 代码如下:  <form action="action.php" method="post" >  &l...