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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php将字符串随机分割成不同长度数组的方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

 strlen($str)) ? strlen($str) : $p;
    $buffer = substr($str,$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将字符串随机分割成不同长度数组的方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。