gpt4 book ai didi

php - PHP使用file_get_contents explode 功能?

转载 作者:行者123 更新时间:2023-12-04 22:37:55 27 4
gpt4 key购买 nike

   <?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>


上面的代码打印一个数组作为输出。

如果我用

<?php
$homepage = file_get_contents('http://www.example.com/data.txt');
print_r (explode(" ",$homepage));
?>


但是,它不会以数组形式在文本文件中显示单个数字。

最终,我想从文本文件中读取数字并打印其频率。 data.txt具有100,000个数字。每行一个数字。

最佳答案

新行不是空格。您必须在适当的换行符组合处爆炸。例如。对于Linux:

explode("\n",$homepage)


或者,可以使用 preg_split和与每个空白字符匹配的字符组 \s

preg_split('/\s+/', $homepage);




另一种选择(可能更快)可能是使用 fgetcsv

关于php - PHP使用file_get_contents explode 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com