<?php

/*
* @version V1.01 16 June 2004 (c) Petar Nedyalkov (bu@orbitel.bg). All rights reserved.
* Released under the GPL license.
*/

function fgetcsvfromline ($line$columnCount$delimiterChar ','$enclosureChar '"') {
    
$regExpSpecialChars = array (
        
"|" => "\\|",
        
"&" => "\\&",
        
"$=> "\\$",
        
"(" => "\\(",
        
")" => "\\)",
        
"^" => "\\^",
        
"[" => "\\[",
        
"]" => "\\]",
        
"{" => "\\{",
        
"}" => "\\}",
        
"." => "\\.",
        
"*" => "\\*",
        
"\\" => "\\\\",
        
"/" => "\\/"
    
);

    
$matches = array();

    
$delimiterChar strtr($delimiterChar$regExpSpecialChars);
    
$enclosureChar strtr($enclosureChar$regExpSpecialChars);

    
$regExp "/^";

    for (
$i 0$i $columnCount$i++) {
        
$regExp .= '('.$enclosureChar.'?)(.*)\\'.(2*$i 1).$delimiterChar// construct the regular expression
    
}

    
$regExp substr($regExp0, (strlen($regExp) - strlen($delimiterChar)))."/"// format the regular expression

    
if (preg_match($regExp$line$matches)) {
        
$result = array();
        for (
$i 1$i count($matches)/2$i++) {
            
$result[$i] = $matches[2*$i]; // get only the fields but not the delimiters
        
}

        return 
$result;
    }

    return 
FALSE;
}

$line   "\"Ma\"rk\"*\"Bergeron\"*\"rocks\"*12345*\"times\"";

$output "The line being parsed is:
    "
.$line."
"
;

if (
$data fgetcsvfromline($line5"*""\"")) {
    
ob_start();
    
print_r($data);
    
    
$matchHash "The result is:
        "
.ob_get_contents();
    
    
ob_end_clean();

    
highlight_string($output.$matchHash);
} else {
    echo 
"Unsupported format!";
}

?>