簡體   English   中英

PHP寫入文本文件的全部內容

[英]PHP Write to text file full contents

首先,這是我的代碼,所以請看:)

<form method="POST">
    <input name="link">
    <button type="submit">></button>
</form>
<title>GET IMAGES</title>
<?php 
if (!isset($_POST['link'])) exit();
$link = $_POST['link'];
echo '<div id="pin" style="float:center"><textarea class="text" cols="110" rows="50">';
function curl($link)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    $result = curl_exec($ch);
    if(curl_errno($ch)){
        return FALSE;
    }
    return $result;
}
$get=array();

//GET TITLE
    $get[$i] = curl($link);
    if (preg_match_all('/">(.*?)<\/a><\/h1>/', $get[$i], $matches))
    foreach ($matches[1] as $title)
    $data = "$title\n";
    echo $data;

//GET IMAGES
for($i=1;$i<=100;$i++)
{
  if ($i == 1) $url = $link;
    else $url = "$link?p=$i";
    $get[$i] = curl($url);
    if (preg_match_all('/<img id="bigImg" src="(.*?)"/', $get[$i], $matches))
    {
        foreach ($matches[1] as $content) {
        $content = str_replace("//img","http://img",$content);
        $data = "<img src=\"".$content."\" />";
        echo $data."\r\n";
        }
    }
  if (!substr_count($get[$i], '下一頁')) break;
}
file_put_contents("1.txt","$data",FILE_APPEND | LOCK_EX);
echo '</textarea>';
?>

當我提交URL時,在文本區域中收到的結果如下:

THIS IS A TITLE 
<img src="https://img.example.com/1.jpg"/> <img
src="https://img.example.com/2.jpg"/> <img
src="https://img.example.com/3.jpg"/>

但是當我使用file_put_contents函數寫入文本文件並打開它以檢查結果時,我只能得到

<img src="https://img.example.com/3.jpg"/>

有任何想法嗎?

您需要附加到$data而不覆蓋它。 使用$data .=而不是$data =因為后者會覆蓋它。

$data .= "<img src=\"".$content."\" />";
//    ^ append

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM