PHP - CSV 到 MySQL 到前端,然后返回 CSV,最佳编码/字符串函数实践?
我有一个场景,其中使用 CSV 文件来维护 MySQL 数据库中的数据(产品)。基本上,客户端维护产品的步骤如下:
- 将项目添加到预定义列中的 CSV 文件中
- 在后端上传 CSV
- PHP 脚本迭代 CSV 数据
- 验证/清理数据并将其发送到 MySQL 数据库
反之亦然客户端编辑、添加或删除项目,但这也可以直接从后端完成。我遇到了某些字符在数据库或下载的电子表格中无法正确显示的问题,所以我的问题是:
一般来说,在以以下形式添加和检索数据时,一组好的编码或字符串函数可以应用于数据: CSV 文件?我的 MySQL 连接编码是 UTF-8,但是我尝试使用 UTF-8 字符集保存 CSV 文件,但 Excel 似乎自动对其应用默认的 Windows-1252 编码,从而破坏了所有 ym UTF-8 字符。
像 Open Office.org 这样的应用程序会提示您输入导入字符集,并且当设置为 Unicode UTF-8 时,它会显示正常。显然,不能依赖总是使用 Excel 或 Open Office 的客户端,那么 PC / CSV 的良好解决方案是什么? MySQL >返回 PC/CSV 工作?
欢迎并赞赏所有建议。
西蒙
I have a scenario where CSV files are being used to maintain data (products) in a MySQL database. Basically the steps that the client follows to maintain products is as follows:
- Adds items to the CSV file in predefined columns
- Uploads the CSV in backend
- PHP script iterates over CSV data
- Data is validated / cleaned and sent to MySQL database
Same applies in reverse when the client edits, adds or removes items, however this can also be done straight from the backend. I have had issues with certain characters not showing properly in the database or in the downloaded spreadsheet, so my question is:
Generally, what is a good set of encoding or string functions to apply to the data when adding and retrieving it in the form of a CSV file? My MySQL connection encoding is UTF-8, however I have tried saving the CSV file with a UTF-8 character set, but it seems Excel automatically applies the default Windows-1252 encoding to it, breaking all ym UTF-8 characters.
Applications like Open Office.org prompt you for the import character set, and whe set to Unicode UTF-8 it displays fine. Obviously one cannot rely on the client always using Excel or Open Office, so what is a good solution for PC / CSV > MySQL > back to PC / CSV work?
All suggestions welcomed and appreciated.
Simon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以在评论后回答。充分理解
http://www.joelonsoftware.com/articles/Unicode.html
并了解如何实现数据映射器模式,有关它的示例文章位于 -http://www. devshed.com/c/a/PHP/Implementing-the-Data-Mapper-Design-Pattern-in-PHP-5
。So to answer after the comments. Have a good understanding of
http://www.joelonsoftware.com/articles/Unicode.html
and look at implementing a datamapper pattern, example article about it here -http://www.devshed.com/c/a/PHP/Implementing-the-Data-Mapper-Design-Pattern-in-PHP-5
.如果我理解正确的话,您正在尝试使用 Excel 打开、编辑和保存 CSV 文件。
这通常是一个坏主意 出于各种原因,包括它不允许您指定详细信息您导出到的 csv 文件的名称。
一种万无一失但更麻烦的方法可能是将数据加载到 Access 中,并在其中处理数据。 Access 允许使用更高级的设置(例如编码和分隔符)“导出到文本文件”。
If i understand correctly, you are trying to open, edit, and save CSV files using Excel.
This is generally a bad idea for a whole range of reasons, including the fact that it does not allow you to specify the details of a csv file you export to.
A fail-safe, yet more cumbersome method might be to load the data into Access instead, and work with the data there. Access allows 'export to text file' using more advanced settings, like encoding and delimiter.