1. ホーム
  2. javascript

CSVから配列、CSVからJSON(JS+PHPデュアルバージョン)

2022-02-09 21:07:10
<パス

I. CSVからJSONへ

function csvToArray(strData, strDelimiter) {
    // Check to see if the delimiter is defined. if not, then default to comma.
    // Check to see if the delimiter is defined. If not, then default to comma.
    strDelimiter = (strDelimiter || ",");
    // Create a regular expression to parse the CSV values.
    // Create a regular expression to parse the CSV values.
    let objPattern = new RegExp((
        // Delimiters.(separator)
        "(\\\" + strDelimiter + "|\\r?\\\n|\\\r|^)" +
        // Quoted fields.
        "(? :\\"([^\"]*(? :\"\"[^\"]*)*)\"|" +
        // Standard fields.
        "([^\"\\\" + strDelimiter + "\\r\\\n]*))"), "gi");
    // Create an array to hold our data. Give the array a default empty first row.
    // Create an array to hold our data. Give the array an array of empty elements
    let arrData = [
        []
    ];
    // Create an array to hold our individual pattern matching groups.
    // Create an array to hold our individual pattern matching groups.
    let arrMatches = null;
    // Keep looping over the regular expression matches until we can no longer find a match.
    // Keep looping over the regular expression matches until we can no longer find a match.
    while (arrMatches = objPattern.exec(strData)) {
        console.log(arrMatches);
        // Get the delimiter that was found.
        // Get the delimiter that was found.
        let strMatchedDelimiter = arrMatches[1];
        // Check to see if the given delimiter has a length
        // Check to see if the given delimiter has a length
        // (is not the start of string) and if it matches
        // (is not the start of string) and if it matches
        // field delimiter. if id does not, then we know
        // field delimiter. If id does not, then we know
        // that this delimiter is a row delimiter.
        // that this delimiter is a row delimiter.
        if (strMatchedDelimiter.length && (strMatchedDelimiter ! = strDelimiter)) {
            // Since we have reached a new row of data, add an empty row to our data array.
            // Since we have reached a new row of data, add an empty row to our data array.
            arrData.push([]);
        }
        // Now that we have our delimiter out of the way,
        // Now that we have our delimiter out of the way.
        // let's check to see which kind of value we
        // Let's check to see which kind of value we need
        // captured (quoted or unquoted).
        // captured (quoted or unquoted).
        let strMatchedValue;
        if (arrMatches[2]) {
            // We found a quoted value. When we capture this value, unescape any double quotes.
            // We found a quoted value. When we capture this value, unescape any double quotes.
            strMatchedValue = arrMatches[2].replace(
                new RegExp("\"\"", "g"), "\"");
        } else {
            // We found a non-quoted value.
            // We found a non-quoted value.
            strMatchedValue = arrMatches[3];
        }
        // Now that we have our value string, let's add it to the data array.
        // Now that we have our value string, let's add it to the data array.
        arrData[arrData.length - 1].push(strMatchedValue);
    }
    // Remove the last empty data
    arrData.splice(-1);
    
    // Return the parsed data.
    // Return the parsed data.
    return (arrData);
}
// Convert json object
function csvToObject(csv) {
    var array = csvToArray(csv);
    var objArray = [];
    for (var i = 1; i < array.length; i++) {
        objArray[i - 1] = {};
        for (var k = 0; k < array[0].length && k < array[i].length; k++) {
            var key = array[0][k];
            objArray[i - 1][key] = array[i][k]
        }
    }
    return objArray;
}
// convert json string
function csvToJson(csv){
	return JSON.stringify(csvToObject(csv));
}

// php version
function csvToArray($strData, $strDelimiter = null){
    // Check if a delimiter is defined. If not, it defaults to a comma.
    $strDelimiter = empty($strDelimiter)? ",":$strDelimiter;
    // Create a regular expression to parse CSV values.
    $objPattern = "/(\\". $strDelimiter."|\\r?\\n|\\\r|^)". // separator
        "(? :\"([^\"]*(? :\"\"[^\"]*)*)\"|". // referenced fields
        "([^\"\\\". $strDelimiter."\\r\\n]*))/i";// Standard fields
    // Create an array to hold the data. Given the array
    // default to an empty first row
    $arrData = [
        []
    ];
    // Create an array to hold our single pattern, the matches group
    $arrMatches = null;
    // Set the offset
    $offset = 0;
    while(preg_match($objPattern, $strData, $matches, PREG_OFFSET_CAPTURE, $offset)){
        // Get the offset
        $offset += mb_strlen($matches[0][0]);
        if(empty($matches[3])){
            continue;
        }
        // Get the delimiter that was found.
        // Get the delimiter that was found.
        $strMatchedDelimiter = $matches[1][0];
        // Check to see if the given delimiter has a length
        // Check to see if the given delimiter has a length
        // (is not the start of string) and if it matches
        // (is not the start of string) and if it matches
        // field delimiter. if id does not, then we know
        // field delimiter. If id does not, then we know
        // that this delimiter is a row delimiter.
        // that this delimiter is a row delimiter.
        if (strlen($strMatchedDelimiter) && ($strMatchedDelimiter ! = $strDelimiter)) {
            // Since we have reached a new row of data, add an empty row to our data array.
            // Since we have reached a new row of data, add an empty row to our data array.
            $arrData[] = [];
        }
        // Now that we have our delimiter out of the way,
        // Now that we have our delimiter out of the way.
        // let's check to see which kind of value we
        // Let's check to see which kind of value we need
        // captured (quoted or unquoted).
        // captured (quoted or unquoted).
        $strMatchedValue = '';
        if ($matches[2][0]) {
            // We found a quoted value. When we capture this value, unescape any double quotes.
            // We found a quoted value. When we capture this value, unescape any double quotes.
            $strMatchedValue = preg_replace('/""/g','\"',$matches[2][0]);
        } else {
            // We found a non-quoted value.
            // We found a non-quoted value.            
            $strMatchedValue = $matches[3][0];
        }
        // Now that we have our value string, let's add it to the data array.
        // Now that we have our value string, let's add it to the data array.
        $arrData[count($arrData) - 1][] = $strMatchedValue;
    }
    // Remove the last empty array
    array_pop($arrData);
    return $arrData;
}



// php version
function csvToArray($strData, $strDelimiter = null){
    // Check if a delimiter is defined. If not, it defaults to a comma.
    $strDelimiter = empty($strDelimiter)? ",":$strDelimiter;
    // Create a regular expression to parse CSV values.
    $objPattern = "/(\\". $strDelimiter."|\\r?\\n|\\\r|^)". // separator
        "(? :\"([^\"]*(? :\"\"[^\"]*)*)\"|". // referenced fields
        "([^\"\\\". $strDelimiter."\\r\\n]*))/i";// Standard fields
    // Create an array to hold the data. Given the array
    // default to an empty first row
    $arrData = [
        []
    ];
    // Create an array to hold our single pattern, the matches group
    $arrMatches = null;
    // Set the offset
    $offset = 0;
    while(preg_match($objPattern, $strData, $matches, PREG_OFFSET_CAPTURE, $offset)){
        // Get the offset
        $offset += mb_strlen($matches[0][0]);
        if(empty($matches[3])){
            continue;
        }
        // Get the delimiter that was found.
        // Get the delimiter that was found.
        $strMatchedDelimiter = $matches[1][0];
        // Check to see if the given delimiter has a length
        // Check to see if the given delimiter has a length
        // (is not the start of string) and if it matches
        // (is not the start of string) and if it matches
        // field delimiter. if id does not, then we know
        // field delimiter. If id does not, then we know
        // that this delimiter is a row delimiter.
        // that this delimiter is a row delimiter.
        if (strlen($strMatchedDelimiter) && ($strMatchedDelimiter ! = $strDelimiter)) {
            // Since we have reached a new row of data, add an empty row to our data array.
            // Since we have reached a new row of data, add an empty row to our data array.
            $arrData[] = [];
        }
        // Now that we have our delimiter out of the way,
        // Now that we have our delimiter out of the way.
        // let's check to see which kind of value we
        // Let's check to see which kind of value we need
        // captured (quoted or unquoted).
        // captured (quoted or unquoted).
        $strMatchedValue = '';
        if ($matches[2][0]) {
            // We found a quoted value. When we capture this value, unescape any double quotes.
            // We found a quoted value. When we capture this value, unescape any double quotes.
            $strMatchedValue = preg_replace('/""/g','\"',$matches[2][0]);
        } else {
            // We found a non-quoted value.
            // We found a non-quoted value.            
            $strMatchedValue = $matches[3][0];
        }
        // Now that we have our value string, let's add it to the data array.
        // Now that we have our value string, let's add it to the data array.
        $arrData[count($arrData) - 1][] = $strMatchedValue;
    }
    // Remove the last empty array
    array_pop($arrData);
    return $arrData;
}



II. JSONからCSVへ

参考
http://csv2json.com/
php の preg_match の正規表現について説明します。