使用 preg_match_all echo 与 file_put_contents
<?php
$start_date = "20111101";
$todays_date = date('Ymd');
$html = file_get_contents("http://online.wsj.com/mdc/public/page/2_3021-tradingdiary2-{$start_date}.html?mod=mdc_pastcalendar");
preg_match_all(
'#<td style="text-align:left;padding-top:18px;" valign="bottom" class="colhead">(.*?)</td>#',
$html,
$EXCHANGE,
PREG_PATTERN_ORDER);
preg_match_all(
'#<td class="num">(\d.\d\d)</td>#',
$html,
$TRIN,
PREG_PATTERN_ORDER);
echo "{$EXCHANGE[0][0]}, \r";
echo "TRIN : {$TRIN[0][0]}, \r";
echo "{$EXCHANGE[0][1]}, \r";
echo "TRIN : {$TRIN[0][1]}, \r";
echo "{$EXCHANGE[0][2]}, \r";
echo "TRIN : {$TRIN[0][2]}, \r";
echo "{$EXCHANGE[0][3]}, \r";
echo "TRIN : {$TRIN[0][3]}, \r";
// write this to a file
$WSJData = 'WJSData.csv';
$WriteMe = "{$TRIN[0][0]}, {$TRIN[0][1]}, {$TRIN[0][2]}, {$TRIN[0][3]}";
file_put_contents($WSJData, $WriteMe );
?>
为什么 WSJData.csv 中的输出与 echo 输出不同?为什么我会在 CSV 文件中看到使用 preg_match_all 函数删除的文本?
<?php
$start_date = "20111101";
$todays_date = date('Ymd');
$html = file_get_contents("http://online.wsj.com/mdc/public/page/2_3021-tradingdiary2-{$start_date}.html?mod=mdc_pastcalendar");
preg_match_all(
'#<td style="text-align:left;padding-top:18px;" valign="bottom" class="colhead">(.*?)</td>#',
$html,
$EXCHANGE,
PREG_PATTERN_ORDER);
preg_match_all(
'#<td class="num">(\d.\d\d)</td>#',
$html,
$TRIN,
PREG_PATTERN_ORDER);
echo "{$EXCHANGE[0][0]}, \r";
echo "TRIN : {$TRIN[0][0]}, \r";
echo "{$EXCHANGE[0][1]}, \r";
echo "TRIN : {$TRIN[0][1]}, \r";
echo "{$EXCHANGE[0][2]}, \r";
echo "TRIN : {$TRIN[0][2]}, \r";
echo "{$EXCHANGE[0][3]}, \r";
echo "TRIN : {$TRIN[0][3]}, \r";
// write this to a file
$WSJData = 'WJSData.csv';
$WriteMe = "{$TRIN[0][0]}, {$TRIN[0][1]}, {$TRIN[0][2]}, {$TRIN[0][3]}";
file_put_contents($WSJData, $WriteMe );
?>
Why is the output in WSJData.csv different from the echo output? Why do I get the text I removed with the preg_match_all function in my CSV file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要将某些
echo
调用的结果写入文件,请使用:这将确保打印的内容就是文件中的内容。
If you want to write the result of some
echo
calls to a file, use:This will ensure that what is printed is what goes in the file.
抱歉,在我看来您的脚本中还有其他错误。我将其简化为“最简单的有效程序”来重现该错误。这是我得到的:
输出是:
第一行输出来自回声。第二个来自“file_get_contents”。
替换效果很好。事实上,它是在分配变量时发生的,而不是在回显或保存到文件时发生的。
顺便说一句,我建议使用 \n 而不是 \r。第二个不会前进一行,而是覆盖当前行(就像在打印机中一样在控制台中),我怀疑这不是您想要的。
Sorry, it seems to me that you have some other error in your script. I reduced it to "the simplest program that works" to reproduce the error. Here is what I got:
And the output is:
The first line of output is from the echoes. The second is from "file_get_contents".
The substitution works fine. Indeed, it occurs when assigning the variables, not when echoing or saving to the file.
BTW, I would recommend using \n instead of \r. The second one will not advance a line, but overwrite the current one (just in the console as it would in the printer), and I suspect that that's not what you wanted.