search
HomeBackend DevelopmentPHP Tutorial读取CSV文件的问题

现在有个CSV的文件,格式如下:

"11111";"1111111";"2324242";"66dggdgg"LF(换行)
“sdfsfsdfsdf”;"sdfsdfsdfsfd";"sdfsdf CR LF(换行) sfsdf";"sdfsdfsdfsfsf"LF(换行)

现在的问题是字段里面的内容包含了换行符,请问这种一般怎么处理?(东南亚一个垃圾平台导出的文件格式)


回复讨论(解决方案)

你应给出真实的文件,哪怕是截个图也好
并说明你遇到的问题

fgetcsv、str_getcsv 都可以很好的处理

http://pan.baidu.com/s/1sjEcrM1
以上是文档链接,主要是怎么处理内容中的换行符

$fp = fopen('Order_2015-12-01_113045.csv', 'r');while($buf = fgetcsv($fp, '"', ';')) {  $res[] = $buf;}print_r($res);
Array(    [0] => Array        (            [0] => 锘�"Order Item Id"            [1] => Lazada Id            [2] => Seller SKU            [3] => Lazada SKU            [4] => Created at            [5] => Updated at            [6] => Order Number            [7] => Customer Name            [8] => National Registration Number            [9] => Shipping Name            [10] => Shipping Address            [11] => Shipping Address2            [12] => Shipping Address3            [13] => Shipping Address4            [14] => Shipping Address5            [15] => Shipping Phone Number            [16] => Shipping Phone Number2            [17] => Shipping City            [18] => Shipping Postcode            [19] => Shipping Country            [20] => Billing Name            [21] => Billing Address            [22] => Billing Address2            [23] => Billing Address3            [24] => Billing Address4            [25] => Billing Address5            [26] => Billing Phone Number            [27] => Billing Phone Number2            [28] => Billing City            [29] => Billing Postcode            [30] => Billing Country            [31] => Payment Method            [32] => Paid Price            [33] => Unit Price            [34] => Shipping Fee            [35] => Wallet Credits            [36] => Item Name            [37] => Variation            [38] => Color            [39] => CD Shipping Provider            [40] => Shipping Provider            [41] => Shipment Type Name            [42] => Shipping Provider Type            [43] => CD Tracking Code            [44] => Tracking Code            [45] => Tracking URL            [46] => Promised shipping time            [47] => Premium            [48] => Status            [49] => Reason        )    [1] => Array        (            [0] => 4510457            [1] => 10940553            [2] => lazada000009            [3] => NO037OTAA43BO8ANMY-8132332            [4] => 2015-11-30 14:09:14            [5] => 2015-11-30 15:23:39            [6] => 344351562            [7] =>             [8] =>             [9] => Rinee Ramiah            [10] => UMW TOYOTSU MOTORS SDN BHDNo 1,jalan batu arang,            [11] =>             [12] =>             [13] =>             [14] =>             [15] => 0122533611            [16] =>             [17] => Selangor-Rawang-48000            [18] => 48000            [19] => Malaysia            [20] => Rinee Ramiah            [21] => UMW TOYOTSU MOTORS SDN BHDNo 1,jalan batu arang,            [22] =>             [23] =>             [24] =>             [25] =>             [26] => 0122533611            [27] =>             [28] => Selangor-Rawang-48000            [29] => 48000            [30] => Malaysia            [31] => Cybersource            [32] => 49.25            [33] => 49.25            [34] => 0.00            [35] => 0.00            [36] => BesWLZ Multi Function Mens Military DualTime Digital Analog Chronograph Sport Wrist Watch 50M Water Resistant Waterproof for Boy Girls Child Kids Gift (Green)            [37] => ...            [38] =>             [39] =>             [40] =>             [41] => Dropshipping            [42] => standard            [43] =>             [44] =>             [45] =>             [46] =>             [47] => No            [48] => canceled            [49] => Out of Stock - Product non available        ))

[0][0] 元素因 BOM 头的影响,结果有点问题
去掉就正常了

Array(    [0] => Array        (            [0] => Order Item Id            [1] => Lazada Id  ......

请问怎么去掉BOM头, 是要用文本编辑软件去掉吗?如果用PHP怎么去掉?

$fp = fopen('Order_2015-12-01_113045.csv', 'r');
fseek($fp, 3); //这样就去掉了
while($buf = fgetcsv($fp, '"', ';')) {
  $res[] = $buf;
}
print_r($res);

也不是去掉,是 跳过

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),