This article describes a method that PHP randomly divides strings into arrays of different lengths. Share to everyone for your reference. The specific analysis is as follows:
This uses PHP to randomly segment the string within a specified length, dividing the result into an array
?
| 1, 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
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:/* * * * * 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 E Xam [3] => ple to [4] => Test T [5] => He [6] => [7] => ran [8] => D_SPL [9] => it F [a] => un [1 1] => ction. ) */ |
I hope this article will help you with your PHP program design.