So I sat down and had a think. I came to the conclusion that what I really needed was to generate a list of possible number combinations. I've written the following code:
if($_POST['submit'] == "Split"){
$split = '2889607315973661';//$_POST['res'];
$sum = '14220';//$_POST['gat'];
checkForMatch($split,$sum);
}
function checkForMatch($num,$tot){
//work out the length of the main number
$numLen = strlen($num);
//we know we HAVE to have 4 numbers, so after we take a random split, we need to be
//100% sure we can make the remaining numbers that total 4
$numbersAssigned = 0;
for($i=3;$i>=0;$i--){
//the next number will be:
// numbers length - numbers we've assigned already - numbers left to make
//the above ensures that after we've made a number, there are enough digits left to make the remaining numbers
$randomSlice = rand(1,$numLen-$numbersAssigned-$i);
//if this is the last number, just assign the unasigned numbers to it as there is no need for a random
if($i==0) $randomSlice = $numLen-$numbersAssigned;
//if we've taken our last number length as 4, increment the total so we can take it into consideration next time
$numbersAssigned += $randomSlice;
echo $randomSlice . "<br/>";
}
echo "<b>$numLen</b>";
}
The output will be 4 numbers, these are the lengths of of the internal 4 numbers and the final is the length of our string. See below an example of the output:
1
8
4
3
16
The problem to note here is that there is no structure to the number splitting, so should there be a large number and the random number generator sucks, we could theoretically go on forever.
I've tested with different lengths of number and it's all working perfectly so far, but as I said, there is no structure at the moment. I'm going to keep messing around as well as expanding the function. I will make it get the numbers using the splits as well as checking that they match the total. I may also implement a while loop to keep checking until a match was found.
I will post more results here when I'm done.