我的PHP foreach循环正确输出print_r但不输出file_put_contents

时间:2015-07-30 06:02:41

标签: php foreach simple-html-dom file-put-contents

我使用simple_html_dom来解析页面并返回一些内容。

当我尝试在foreach循环中输出带有print_r的内容时,它会返回所有元素。但是,如果我尝试将内容输出到文本文件,它只输出最后一个元素。我做错了什么?

以下是我的示例代码:

<div id="img-container">
    <img id="cropimage" />
</div>
<div>
    <img src="" id="img1" />
    <img src="" id="img2" />
    <img src="" id="img3" />
    <img src="" id="img4" />
    <img src="" id="img5" />
</div>

print_r输出示例:

include 'simple_html_dom.php';

$partlist_file = $_SERVER['DOCUMENT_ROOT'].'/partlist.txt';

$partlist = file('knn-partnumberlist.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$stock = '';

    $output_first = '';
    foreach($partlist as $parts => $part) {

        $html = file_get_html('search/product.aspx?prod=' . $part);
        $ret = $html->find('span#cph_lb_stock_buy'); 

        foreach($ret as $element) {
            $stock = $element->plaintext;
            $stock = preg_replace(array('/\\n/','/\\r/'),'',$stock);
            $stock = trim($stock);
            if($stock == 'Not in stock.') {
                $stock = '0';
            } elseif($stock == 'In Stock & Ready to Ship!') {
                $stock = '6';
            }



        $output = 'K33' . $part . ',' . $stock . "\n";
        print_r ($output); // returns all elements
        file_put_contents($partlist_file, $output); // only returns last element

        }
    }

file_put_contents输出示例:

K3300283,6
K3301518,6
K3301988,6
K3303351,6
K3303365,6

2 个答案:

答案 0 :(得分:1)

你需要为每次迭代连接$ output,而不是简单地覆盖它

 $output .= 'K33' . $part . ',' . $stock . "\n";
        print_r ($output);
        file_put_contents($partlist_file, $output);

.是连接运算符

答案 1 :(得分:0)

print_r函数有一个布尔选项,允许它作为字符串变量返回,而不是将其打印到屏幕上。它的用法如下:

$myVar = print_r($otherVar, true);

这将允许您收集值,而不是直接将其打印到输出缓冲区(例如屏幕)。这将证明是有用的,因为您现在可以根据需要或期望操纵值。

此外,file_put_contents还有一个选项,允许您附加到文件,而不是覆盖它的当前内容。再一次,用法看起来像这样:

$ dummy = file_put_contents(&#39; path / to / file&#39;,$ varToSave,FILE_APPEND);

最后一个选项是一个整数位标志(我不知道它的实际值)告诉PHP将新数据添加到文件的末尾而不是用新的替换当前内容数据。在原始代码中使用这两个选项都是这样的:

include 'simple_html_dom.php';

$ partlist_file = $ _SERVER [&#39; DOCUMENT_ROOT&#39;]。&#39; /partlist.txt' ;;

$ partlist = file(&#39; knn-partnumberlist.txt&#39;,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$ stock =&#39;&#39;;

$output_first = '';
foreach($partlist as $parts => $part) {

    $html = file_get_html('search/product.aspx?prod=' . $part);
    $ret = $html->find('span#cph_lb_stock_buy'); 

    foreach($ret as $element) {
        $stock = $element->plaintext;
        $stock = preg_replace(array('/\\n/','/\\r/'),'',$stock);
        $stock = trim($stock);
        if($stock == 'Not in stock.') {
            $stock = '0';
        } elseif($stock == 'In Stock & Ready to Ship!') {
            $stock = '6';
        }



    $output = 'K33' . $part . ',' . $stock . "\n";
    $display = print_r ($output, true);
    // you can manipulate $display here, like so:
    $display = str_replace("\n", "\r\n", $display);
    echo $display; // returns all elements (one at a time)
    file_put_contents($partlist_file, $display, FILE_APPEND); // now stores all values of $output in the file

    }
}

当然,看起来你并不真的需要在脚本中操作$ output的值,但是为了说明print_r中选项的用处,我添加了一行代码替换使用Windows换行符(\ r \ n)的Linux换行符(\ n),以便记事本等程序可以正确显示文本(PHP的print_r函数只在输出中放置\ n换行符,即使在Windows机器上也是如此,这是为什么我发现这篇文章。我试图找到一种方法来纠正这种行为)。这对您来说可能不会对您有用,但它可能会在以后发生,并且可能对遇到此问题的其他人有用。我希望这有帮助。 :)