php - file_get_contents() 或类似函数
我正在使用 file_get_contents() 检索我存储在 .txt 文件中的县列表。每个县都有自己的线路。
有没有办法在函数中的每个条目周围添加引号,然后在其后面添加逗号?我的一个限制是它必须在函数内填充,并且不能是两个单独的命令。
这可以做到吗?
或者也许我需要为此研究另一个函数?
I'm using file_get_contents() to retrieve a list of counties that I have stored in a .txt file. Each county is on its own line.
Is there a way to add quotations around each entry and then a comma following it within the function? One restriction I have is that it would have to populate within the function, and cannot be two separate commands.
Can this be done?
Or maybe I need to look into another function for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以很容易地做到这一点,如果你使用
file
而不是file_get_contents
:我用对 PHP 有效的语法编写了
$callback
$callback
5.3;也可以将其编写为匿名函数:查看实际操作。
You can do it pretty easily, and it's even better if you use
file
instead offile_get_contents
:I have written
$callback
with a syntax that is valid for PHP < 5.3; it's also possible to write it as an anonymous function:See it in action.
如果您逐行阅读某些内容,
文件
比file_get_contents
更合适:If you read something line-by-line,
file
is more appropriate thanfile_get_contents
:当然。
这应该将文件像这样转换
成这样的数组:
这就是你想要的,对吗?
Sure.
That should convert the file like this:
into an array like this:
That's what you want, right?