<?php
ini_set('display_errors', 0);
$rawstring		= trim($_POST['rawstring']);
$seqlen 		= (int) trim($_POST['seqlen']);
$delim			= $_POST['delim'];//don't trim this

if($rawstring!="" && $seqlen>0 && $delim!=""){
	//echo "<br />'SEQUENCE SPLITTER' Started<pre>";
	$sarr = explode($delim, $rawstring);

	// $ptr = 0;
	// do{
	// 	for($j = 0; $j < $seqlen; $j++){
	// 		$s = trim($sarr[$ptr+$j]);
	// 		if($s==''){//encountered extra space
				
	// 		}else{
	// 			if($j == 0) echo $s; else echo ",$s";
	// 			$ptr = $ptr + $seqlen;
	// 		}
	// 	}
	// 	echo "<br />";
	// } while($ptr < sizeOf($sarr));

	$holding = [];
	for($i = 0; $i <= sizeOf($sarr); $i++){
		$s = trim($sarr[$i]);
		if($s!=''){
			$holding[] = $s;
			if(sizeOf($holding) == $seqlen){
				for($j = 0; $j < $seqlen; $j++){
					$t = $holding[$j];
					if($j == 0) echo $t;
					else echo ",$t";
				}
				$holding = [];
			}
		}
		echo "<br />";
	}


	// for($i = 0; $i <= sizeOf($sarr); $i += $seqlen){
	// 	for($j = 0; $j < $seqlen; $j++){
	// 		$s = trim($sarr[$i+$j]);
	// 		if($s!=''){
	// 			if($j == 0) echo $s; else echo ",$s";
	// 		}
	// 	}
	// 	echo "<br />";
	// }

	echo "</pre><br />SEQUENCE SPLITTER ENDED<hr />";
}else{
	echo "Incorrect value(s), please check";
}
?>