PHP 字符串因间距而爆炸的问题
我正在尝试使用空格作为分隔符来创建一个非常简单的列表爆炸。但是,我对以下字符串有一些问题...
"+0.59 - +0.58%" "+0.06 - +0.14%" "-0.47 - -1.07%" "-0.77 - -0.20%" //输入
和结果数组应该由每个空格分隔(引号也被删除)
Array ( [0] => +0.59 [1] => - [2] => +0.58% +0.06 [3] => - [4] => +0.14% -0.47 [5] => -1.07% -0.77 [7] => - [8] => -0.20% )
基本上空格未被正确识别。我已经尝试通过 /n /r 和 '/\s*/m' 来分隔它。
这是我的代码片段。
$open = fopen("http://finance.yahoo.com/d/quotes.csv?s=$ticker&f=c&e=.csv", "r");
$quote = fread($open, 2000);
fclose($open);
$quote = explode(" ", $quote);
foreach ($quote as &$value) {
$value = str_replace('"',"",$value);
}
//print_r($tickerlist);
print_r($quote);
I am trying to do a very simple list explode using spaces as the delimiters. However, I am have some problems with the following string...
"+0.59 - +0.58%" "+0.06 - +0.14%" "-0.47 - -1.07%" "-0.77 - -0.20%" //Input
And the resultant array which is supposed to be separated by each space (quotes also removed)
Array ( [0] => +0.59 [1] => - [2] => +0.58% +0.06 [3] => - [4] => +0.14% -0.47 [5] => - [6] => -1.07% -0.77 [7] => - [8] => -0.20% )
Basically the spaces aren't being recognized correctly. I have already tried separating it via /n /r and '/\s*/m'.
Here is a snippet of my code.
$open = fopen("http://finance.yahoo.com/d/quotes.csv?s=$ticker&f=c&e=.csv", "r");
$quote = fread($open, 2000);
fclose($open);
$quote = explode(" ", $quote);
foreach ($quote as &$value) {
$value = str_replace('"',"",$value);
}
//print_r($tickerlist);
print_r($quote);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在适当的编辑器中打开一个文件(vim 会很适合这个。也许是 notepad++)并检查
tab
字符和\r
和\n
。Open a file in a proper editor (vim would be nice for this. maybe notepad++) and check for
tab
character and\r
and\n
.这行得通吗?
Would this work?
如果要转换数据,只需使用此函数将 csv 文件解析为数组即可。
这只会解析关联形式的所有内容并返回该数组。
If you want to convert the data, you can simply use this function to parse the csv file into an array.
this will just parse everything in its associative form and return you that array.