繁体   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