Created
January 19, 2012 06:16
-
-
Save k-holy/1638314 to your computer and use it in GitHub Desktop.
str_getcsv()の第4引数($escape)の挙動
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
assert_options(ASSERT_ACTIVE, 1); | |
assert_options(ASSERT_WARNING, 0); | |
assert_options(ASSERT_CALLBACK, function ($file, $line) { | |
echo '<pre>' . htmlspecialchars(sprintf("Assertion Failed: at %s[%d]\n", $file, $line)) . '</pre>'; | |
}); | |
assert(array('1', 'foo"') == str_getcsv("1\t\"foo\"\"", "\t", '"')); // OK | |
assert(array('1', 'foo"') == str_getcsv("1\t\"foo\\\"", "\t", '"', '\\')); // NG | |
// なぜなの… | |
assert(array('1', 'foo@') == str_getcsv('1,@foo@@@' , ',' , '@')); // こっちはOK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHPマニュアルには第4引数のデフォルトはバックスラッシュって書いてるけど、デフォルトは第3引数と同じ文字になるっぽい
ダブルクォート、カンマ、スペース、改行の挙動を調べた限りでは今のところstr_getcsv()はデフォルト設定でExcel準拠な感じだから、enclosureと違うescapeを指定する必要がなければstr_getcsv()かなあ
自分で書く場合、正常系の処理はともかく、ダブルクォートの数がペアになってないとか、カンマの前後にスペースとか、駄目な場合もExcelと同じ挙動にするのが結構たいへん…