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へ
関連
-
[解決済み】javascriptでのアナグラムファインダー
-
[解決済み】Eslint:Node.jsで「予期しないコンソールステートメント」を無効にする方法とは?
-
[解決済み] Uncaught TypeError: 未定義のプロパティ '0' を設定できない "
-
[解決済み] Javascriptでオブジェクトを作成する - 複数のアプローチ、何か違いは?
-
[解決済み] Uncaught TypeError: Cannot create property 'guid' on string ' [duplicate] (文字列のプロパティ 'guid'を作成できない)。
-
[解決済み] JavaScriptでTinymceのtextareaの内容を取得する方法
-
[解決済み] Firebaseで複数のアイテムをプッシュするには?
-
[解決済み] Uncaught TypeError: $(...).tooltip は関数ではありません。
-
[解決済み] "document.getElementByClassは関数ではありません"
-
[解決済み] ko.validation.group関数の使用方法
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
[解決済み】リソースの読み込みに失敗しました:net::ERR_CONTENT_LENGTH_MISMATCH
-
[解決済み] PythonでSeleniumを使って人間認証「プレスアンドホールド」を回避する方法とは?
-
[解決済み] Uncaught TypeError: switch関数の先頭で未定義のプロパティ'type'を読み取れない
-
[解決済み] 未定義のプロパティ 'protocol' を読み取ることができません。
-
[解決済み] 要素外でのクリックを検出するにはどうすればよいですか?
-
[解決済み] Phaser 3でのゲームの一時停止と再開
-
[解決済み] setAttributeで設定されたonclickプロパティがIEで機能しないのはなぜですか?
-
[解決済み] 浮動小数点数が他の浮動小数点数より何ポイント大きいか [閉じた状態]
-
[解決済み] contenteditableを使用したonChangeイベント [重複]。
-
Uncaught SyntaxError.とは何ですか?Unexpected end of inputとはどういう意味ですか?