There is a txt file with some text:
1 2 3 4 5 6 7 8 9 It is necessary to make it happen so:
1 2 3 4 5 6 7 8 9 If the source file is file_1.txt , and the file with the result file_2.txt and we are talking about replacing one space, then we can do this:
file_put_contents('file_2.txt', ''); foreach (file('file_1.txt') as $row) { file_put_contents('file_2.txt', str_replace(' ', "\n", $row), FILE_APPEND); } And I will take part:
$withSpaces = file_get_contents('source.txt'); $noSpaces = str_replace(' ', PHP_EOL, $withSpaces); file_put_contents('dest.txt', $noSpaces); In short?
file_put_contents('dest.txt', str_replace(' ', PHP_EOL, file_get_contents('source.txt'))); This is quite simple:
$handle = fopen("input.txt", "r"); $handle_w = fopen("output.txt", "w"); if ($handle) { while (($line = fgets($handle)) !== false) { $line = str_replace ( ' ' , "\n" , $line ); fputs($handle_w, $line); } fclose($handle); fclose($handle_w); } else { echo 'Ошибка чтения файла'; } Source: https://ru.stackoverflow.com/questions/632925/
All Articles