php 字符串随机分割成不同长度的数组示例

前端之家收集整理的这篇文章主要介绍了php 字符串随机分割成不同长度的数组示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP对字符串在指定的长度范围内进行随机分割,把分割后的结果存在数组里面,PHP将字符串随机分割成不同长度的数组,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:
  1. /**
  2. * 对字符串在指定的长度范围内进行随机分割
  3. *
  4. * @param
  5. * @arrange (512.笔记) jb51.cc
  6. **/
  7. function RandomSplit($min,$max,$str){
  8. $a = array();
  9. while ($str != ''){
  10. $p = rand($min,$max);
  11. $p = ($p > strlen($str)) ? strlen($str) : $p;
  12. $buffer = substr($str,$p);
  13. $str = substr($str,$p,strlen($str)-$p);
  14. $a[] = $buffer;
  15. }
  16. return $a;
  17. }
  18. //范例:
  19. /*
  20. ** Example:
  21. */
  22. $test_string = 'This is a example to test the RandomSplit function.';
  23. print_r(RandomSplit(1,7,$test_string));
  24. /*
  25. Outputs something like this
  26. (Array items are 1 to 7 characters long):
  27. Array
  28. (
  29. [0] => This
  30. [1] => is
  31. [2] => a exam
  32. [3] => ple to
  33. [4] => test t
  34. [5] => he
  35. [6] =>
  36. [7] => ran
  37. [8] => d_spl
  38. [9] => it f
  39. [10] => un
  40. [11] => ction.
  41. )
  42. */
  43. /*** 来自编程之家 jb51.cc(jb51.cc) ***/

猜你在找的PHP相关文章