Anyone have an idea what the regex would be for preg_split to split a string at a semicolon ( ;
), but ignore any quoted (single or double) parts as well as ignore escaped ( \;
) semicolons?
I have tried to decipher this one and could not (regex not my strongpoint - YET)
Herewith is a function I wrote that does this what I want.
//split string into characters and process...
function split_at($input, $splitAt=";"){
$on=1;
$j=0;
$output[0]="";
for($i=0; $i<strlen($input); $i++) {
//when a quote is reached then set $on switch off to ignore quoted part
//untill another quote is reached then it switches $on on again
if($input[$i]=='"' or $input[$i]=="'"){
if ($on) {$on=0;} else {$on=1;}
}
//create new array for new part when a semicolon is reached
//or ignore if in quoted part or if escaped
if($input[$i]==";" && $on==1){
if(isset($input[$i-1]) && $input[$i-1]!="\\"){
$output[++$j]="";
continue;
}
}
$output[$j].=$input[$i];
}
// array of split parts of input
return $output;
}
Any help would be appreciated!