PHP 写入文本文件的全部内容

  • 本文关键字:全部 文件 文本 PHP php
  • 更新时间 :
  • 英文 :

首先

,这是我的代码,所以看看:)

<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 = "$titlen";
    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."rn";
        }
    }
  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

相关内容

最新更新