使用 preg_match_all echo 与 file_put_contents

发布于 2024-12-13 08:26:51 字数 1066 浏览 0 评论 0原文

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2024-12-20 08:26:51

如果要将某些 echo 调用的结果写入文件,请使用:

ob_start(create_function('$a','file_put_contents(FILENAME,$a); return $a;'));
echo ...
echo ...
...
echo ...
ob_end_flush();

这将确保打印的内容就是文件中的内容。

If you want to write the result of some echo calls to a file, use:

ob_start(create_function('$a','file_put_contents(FILENAME,$a); return $a;'));
echo ...
echo ...
...
echo ...
ob_end_flush();

This will ensure that what is printed is what goes in the file.

溺深海 2024-12-20 08:26:51

抱歉,在我看来您的脚本中还有其他错误。我将其简化为“最简单的有效程序”来重现该错误。这是我得到的:

<?php

  $TRIN = array( array("E0","E1","E2","E3") );

  echo "TRIN : {$TRIN[0][0]} / ";
  echo "TRIN : {$TRIN[0][1]} / ";
  echo "TRIN : {$TRIN[0][2]} / ";
  echo "TRIN : {$TRIN[0][3]} \n";

  // 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 );

  echo file_get_contents($WSJData);

输出是:

$ php test.php
TRIN : E0 / TRIN : E1 / TRIN : E2 / TRIN : E3
E0, E1, E2, E3

第一行输出来自回声。第二个来自“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:

<?php

  $TRIN = array( array("E0","E1","E2","E3") );

  echo "TRIN : {$TRIN[0][0]} / ";
  echo "TRIN : {$TRIN[0][1]} / ";
  echo "TRIN : {$TRIN[0][2]} / ";
  echo "TRIN : {$TRIN[0][3]} \n";

  // 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 );

  echo file_get_contents($WSJData);

And the output is:

$ php test.php
TRIN : E0 / TRIN : E1 / TRIN : E2 / TRIN : E3
E0, E1, E2, E3

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文