如何在字符串中使用fgetcsv

编程入门 行业动态 更新时间:2024-10-26 00:29:31
本文介绍了如何在字符串中使用fgetcsv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用 fgetcsv ,其中输入是字符串而不是资源?

How to use fgetcsv where the input is a string and not a resource?

如何转换

fgetcsv 需要文件句柄资源

$str = "1981;2992;19191\n392;488;299\n"some\ntext";199;222"; $array = fgetcsv($str);

不能使用 str_getcsv 因为我有在 str_getcsv 之前用 \n 分隔字符串。如果某些字段包含 \n ,则输出将不正确

Can not use str_getcsv because then I have to split the string by \n before str_getcsv.. If some of the fields contains \n then the output will be incorrect

推荐答案

有 str_getcsv 函数,它可以将CSV字符串解析为数组。

There is str_getcsv function, it can parse CSV string into an array.

更新:如果无法使用 str_getcsv 函数,请尝试将字符串转换为流,并在 fgetcsv :

Update: if you cannot use str_getcsv function, try to convert string to stream and use it in fgetcsv:

$stream = fopen('php://memory', 'r+'); fwrite($stream, '1981;2992;19191\n392;488;299'); rewind($stream); $array = fgetcsv($stream);

更多推荐

如何在字符串中使用fgetcsv

本文发布于:2023-10-27 02:51:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何在   fgetcsv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!