Hello.

There is a script that writes information to a file, and there is another (script) that deletes it when needed.

I need a function to remove spaces and line breaks directly from the file, I tried trim() , but this is sort of for the line, not for the file itself.

Would you recommend that?

Maybe I forgot about some kind of function. Example

 if(file_put_contents($admfile, PHP_EOL . $admexphrase, FILE_APPEND)) { echo "succesadm"; } 

// write to file with line break

// what happened

 1- <p><a href="admintour110.php">tour23.php</a></p> 2- <p><a href="admintour115.php">tour24.php</a></p> 3- <p><a href="admintour120.php">tour25.php</a></p> 

// this is what happens when you delete this line

 1- <p><a href="admintour110.php">tour23.php</a></p> 2- <p><a href="admintour115.php">tour24.php</a></p> 3- 

// when re-recording

 1- <p><a href="admintour110.php">tour23.php</a></p> 2- <p><a href="admintour115.php">tour24.php</a></p> 3 - 4- <p><a href="admintour120.php">tour25.php</a></p> 

// if you delete the last line again and overwrite it will be released

// when re-recording

 1- <p><a href="admintour110.php">tour23.php</a></p> 2- <p><a href="admintour115.php">tour24.php</a></p> 3- 4- 5- <p><a href="admintour120.php">tour25.php</a></p> 

// delete with str_replace

 $admstring = '<p><a href="admintour120.php">tour25.php</a></p>'; $admcontold = array("$admstring"); $admcontnew = array(""); $admphrase = str_replace($admcontold, $admcontnew, $admcont); if(file_put_contents($admlist, $admphrase)) { echo "succes page\n"; } 
  • one
    Give an example of input and output data - E_p
  • added an example above - hovdev
  • $admstring = '<p><a href="admintour120.php">tour25.php</a></p> . PHP_EOL'; - E_p
  • right now I will try, I will write the answer - hovdev
  • Unfortunately it did not work .. - hovdev

3 answers 3

There is another such option without replacing the lines:

test.txt

 line 1 line 2 line 3 

test.php

 <?php // read $data = file('test.txt', FILE_IGNORE_NEW_LINES); // remove $idx = array_search('line 1', $data); if ($idx !== false) { unset($data[$idx]); } // add $data[] = 'brand new line'; // write file_put_contents( 'test.txt', join(PHP_EOL, $data) ); 
  • thanks, I'll try and see how it works - hovdev
  • one
    As an addition to the answer, try sqlite ... And there is nothing to put out the brakes @ S1lllver - Naumov
  • one
    @Naumov I already advised the author of the question to work with the database. Says not suitable. - E_p
  • one
    Well, sqlite writes to the file. I therefore saw a plus for you, and the message to the author was addressed. - Naumov

When you write to a file, you write with PHP_EOL , and when you delete it, without it. It is enough to add PHP_EOL to the uninstall script, and the deletion code will look something like this:

$admcontold = PHP_EOL . $admstring;

I think your option will be costly to the server. If the file is large, it may not fit into the allocated memory, and regular expressions will take a lot of time.

    found a good code that suits you

    Specify the path to the file in which we want to establish order, remove extra spaces and line breaks and everything works. Code Below:

     $c = 'content/text.php'; //читаем отсюда $cont = file_get_contents($c); $cont = trim(preg_replace("/[\r\n]+/m","\r\n", $cont)); file_put_contents('content/test.php', $cont); //записываем сюда 

    // you can read and write to the same file, so I did. Thanks to everyone who tried to help above, I put pluses.