将 CSV 数据分解为数组的函数 - ○白子○ 的Blog

将 CSV 数据分解为数组的函数

[ 2008-01-31 05:41:58 | Author: seaboat ]
Font Size: Large | Medium | Small
在网上找到一个将 CSV 数据行分解为数组的PHP函数,如下:
function splitCsvLine($str) {
 $expr = "/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
 $results = preg_split($expr, trim($str));
 return preg_replace("/^\"(.*)\"$/", "$1", $results);
}
由于利用了正则表达式,函数很简洁,功能却是不弱,带引号和不带引号的 CSV 数据,都可以正确分解。
[Last Modified By seaboat, at 2008-02-21 01:05:40]
Comments Feed Comments Feed: http://www6.webng.com/seaboat/feed.asp?q=comment&id=683

View Mode: Show All | Comments: 2 | Trackbacks: 0 | Toggle Order | Views: 637
Quote 风子
[ 2008-02-11 01:18:25 ]
似乎应该注明是php实现吧?:P
Quote seaboat
[ 2008-02-21 01:06:36 ]
[yes] 多谢提醒。
加上了。

Post Comment
Smilies
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password: