Viewed   79 times

The closest I've seen in the PHP docs, is to fread() a given length, but that doesnt specify which line to start from. Any other suggestions?



You not going to be able to read starting from line X because lines can be of arbitrary length. So you will have to read from the start counting the number of lines read to get to line X. For example:

$f = fopen('sample.txt', 'r');
$lineNo = 0;
$startLine = 3;
$endLine = 6;
while ($line = fgets($f)) {
    if ($lineNo >= $startLine) {
        echo $line;
    if ($lineNo == $endLine) {
Thursday, November 17, 2022


foreach($matches[1] as $content)
     echo $content."rn";

only iterates over the array and makes $content the last element (you have no {} so it is a one liner).

Simple demo of your issue,

You could use use implode though.

fwrite($file,implode("nr", $matches[1]));

You also could simplify this by using file_put_contents. Per the manual:

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

So you could just do:

$re = '/<li><a href="(.*?)"/';
$str = '
<li><a href=""</a></li>
                        <li><a href=""</a></li>
                        <li><a href=""</a></li> ';

preg_match_all($re, $str, $matches);
echo '<div id="pin" style="float:center"><textarea class="text" cols="110" rows="50">';
file_put_contents("1.txt", implode("nr", $matches[1]));
Saturday, November 12, 2022

Thanks CBroe and IVO GELOV, The issue was in my FTP/SFTP Client WinSCP. I attached a screenshot which setting you need to turn off so this problem doesn't happen.

Sunday, December 18, 2022

The conceptual problem here is that files are just strings of characters, some of those characters denote ends of lines. Because of that, it is impossible to know where lines begin and end without reading the file first.

If a file is to be read constantly, you scan the file first and record the offsets for the lines into some kind of index and use fseek() and fread() to read exactly the lines you want.

As you mentioned, databases can do a similar job for you so, instead of creating, essentially, your own database, you could read the file line–by–line and insert those lines in database with some field storing the line number and then get the lines you want with a query.

Monday, November 14, 2022
myFile.renameTo(new File("/the/new/place/newName.file"));

File#renameTo does that (it can not only rename, but also move between directories, at least on the same file system).

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

If you need a more comprehensive solution (such as wanting to move the file between disks), look at Apache Commons FileUtils#moveFile

Tuesday, September 27, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :