<?php
$segments = [1,2,3,4,5,6,7,8,9,1];
$box_size = 4;
$input = 'Loremipsumdolorsitametconsecteturadipiscingeli';
$output = [];
function string_to_stream($string) {
$in = fopen('php://memory','r+');
fwrite($in, $string);
rewind($in);
return $in;
}
$stream = string_to_stream($input);
$left_over = '';
while (($segment = array_shift($segments)) !== null) {
while (strlen($left_over) < $segment) {
$left_over .= fread($stream, $box_size);
}
$output[] = substr($left_over, 0, $segment);
$left_over = substr($left_over, $segment);
}
if (implode('', $output) === $input) {
echo 'Algorithm Correct !' . PHP_EOL . PHP_EOL;
}
foreach ($output as $v) {
echo $v . PHP_EOL;
}
preferences:
49.01 ms | 402 KiB | 5 Q