📜 ⬆️ ⬇️

How to remove blank lines in txt file using a script in php.

Yesterday I had to face one little problem.
I had to put together a base of synonyms so that each line corresponded to one pair. After certain manipulations, I got bases of this type:

and if | well, and if
but in general | in short
but you did not know | really you did not know
not | but not
and then | after which
and also
and even | maybe

and you want to no | but you can not
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
absurd | nonsense
absurdity | absurdity
absurd | crazy
absurd | crazy
')
M, the question arose how to delete blank lines. What I did not do, how I did not try, climbed the floor of the Internet, I couldn’t think of anything. Well, I turned up a simple, but rescued me script. He helped me a lot, so I decided to post it for everyone.
It is simple as a boot, but you know, when you need felt boots, they are not at hand!

<? php
$ url = "base5.txt";
$ link = "$ url";
$ file = fopen ($ link, "r");
if ($ file)
{
$ rf = fread ($ file, filesize ($ link));
fclose ($ file);
}
else
{
echo "fuck";
fclose ($ file);
exit;
}
$ txt = trim (preg_replace ('/ [\ r \ n] + / m', "\ n", $ rf));
$ openFile = fopen ($ url, 'w +');
fwrite ($ openFile, $ txt);
fclose ($ openFile)
?>

I hope he will help you! Thanks for attention!

Source: https://habr.com/ru/post/45014/


All Articles