new2code Posted January 31, 2010 Share Posted January 31, 2010 Hi Im very new to php. I want to open a file (*.txt) and check for all the blank lines and remove them. and save it as the same file or a new file. I know this can be done using regex. although, I cant seemt to implement regex within a php file. <?php $file='logz.txt'; $fe = @fopen("$file", "w"); $fe = preg_replace('^\r?\n','',$fe); fclose($fe); ?> this is what I have, (it might be wrong) but it does not change anything in the logz.txt I will very greatfull if someone can help me out. Quote Link to comment https://forums.phpfreaks.com/topic/190472-open-txt-file-remove-all-blank-lines-and-save-txt-file/ Share on other sites More sharing options...
mattal999 Posted January 31, 2010 Share Posted January 31, 2010 You never save the text back to the text file. Take a look at fwrite(). Quote Link to comment https://forums.phpfreaks.com/topic/190472-open-txt-file-remove-all-blank-lines-and-save-txt-file/#findComment-1004713 Share on other sites More sharing options...
wildteen88 Posted January 31, 2010 Share Posted January 31, 2010 fopen only returns a handle, it doesn't return the text in the file. To read the text from the file you need to use fread. To write to the file you'll need to use fwrite. Seeing as you're reading and writing to the file you'll need to use w+ as the file mode parameter , rather than using w Quote Link to comment https://forums.phpfreaks.com/topic/190472-open-txt-file-remove-all-blank-lines-and-save-txt-file/#findComment-1004719 Share on other sites More sharing options...
teamatomic Posted January 31, 2010 Share Posted January 31, 2010 $file='./links.txt'; $str=file_get_contents("$file"); $str = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $str); file_put_contents("$file", "$str"); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/190472-open-txt-file-remove-all-blank-lines-and-save-txt-file/#findComment-1004728 Share on other sites More sharing options...
salathe Posted January 31, 2010 Share Posted January 31, 2010 A quick (in terms of lines of code at least) would be: file_put_contents('logs.txt', implode(PHP_EOL, file('logs.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES))); If the file is particularly large (i.e. if loading it into memory isn't trivial) then it would be better to go through the file in pieces (like, line-by-line), writing non-empty lines to a temporary file then moving said temporary file to the original location once the process is complete. Quote Link to comment https://forums.phpfreaks.com/topic/190472-open-txt-file-remove-all-blank-lines-and-save-txt-file/#findComment-1004765 Share on other sites More sharing options...
new2code Posted February 1, 2010 Author Share Posted February 1, 2010 Thank you all. teamatomic! your script works fine, I cant believe I couldent figure this out. salathe, one line ! Wow! thank you. you guys are geniuses. works perfectly. I'm at a very basic level trying to learn PHP. Quote Link to comment https://forums.phpfreaks.com/topic/190472-open-txt-file-remove-all-blank-lines-and-save-txt-file/#findComment-1004807 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.