I’m writing a csv file important script. There are two separators that can be selected, comma or semi colon. I’ve noticed though that in the text file there are semi colons and commas in the middle of some of the text. Each text field has speech marks around it but some of the fields are blank and some are numeric. I’m looking for a way to only split text that’s outside the speech marks while leaving text instead the speech marks intact.
Here is the current function I’m using to split up the row.
function parse_lines($p_CSVLines,$c_Names,$separator)
{
$content = FALSE;
if( !is_array($content) ) { // the first line contains fields numbers
$this->fields = $c_Names;
$content = array();
}
foreach($p_CSVLines as $line_num => $line){
if($line != ''){ // skip empty lines
$elements = split($separator, $line);
$item = array();
foreach($this->fields as $id => $field){
if( isset($elements[$id]) ){
$field = trim($field,"\"");
$item[$field] = trim($elements[$id],"\"");
}
}
$content[] = $item;
}
}
return $content;
}
Please note that the use of
split()is highly discouraged, it’s now deprecated. You could usepreg_split()orexplode().What you want to do here is to use REGEX to search for the semi-colons and commas that aren’t in the middle of the your text.
preg_split()should get the job done with the right expression.See https://www.php.net/manual/en/function.preg-split.php