How to remove empty lines in a txt file using php script.
Yesterday I had to face one small problem.
I had to collect a database of synonyms, so that each line corresponds to one pair. After some manipulation I got a database of this kind:
and if | but if
a general | short
and you do not know | do you still do not know
and do not | not
and only then | after that and
as well as | and also
a that and maybe maybe
you want not | maybe not
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
nonsense | nonsense
nonsense | absurdity
absurd | delusional
absurd | delusional
M Here the question arose of how to remove empty lines. What I did not do, if I did not try, I climbed the floor of the Internet, so I couldn’t come up with anything. Well, one simple, but help me script turned up. He helped me a lot, so I decided to put it out for everyone.
It is simple as a felt boot, but you yourself understand, when you need felt boots they are never at hand!
$ 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 it helps you! Thanks for attention!
I had to collect a database of synonyms, so that each line corresponds to one pair. After some manipulation I got a database of this kind:
and if | but if
a general | short
and you do not know | do you still do not know
and do not | not
and only then | after that and
as well as | and also
a that and maybe maybe
you want not | maybe not
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
abstract | abstract
nonsense | nonsense
nonsense | absurdity
absurd | delusional
absurd | delusional
M Here the question arose of how to remove empty lines. What I did not do, if I did not try, I climbed the floor of the Internet, so I couldn’t come up with anything. Well, one simple, but help me script turned up. He helped me a lot, so I decided to put it out for everyone.
It is simple as a felt boot, but you yourself understand, when you need felt boots they are never at hand!
$ 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 it helps you! Thanks for attention!