Re: [PHP] Remove blank lines from a file
At 2:48 PM -0400 5/23/10, Robert Cummings wrote: tedd wrote: At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :) Rob: Okay, so I didn't correctly identify and understand the problem from the client (the list), but my solution worked for the problem I understood. Whereas: Doh, what's funny is I fixed it in my test script but had already pasted into my email and forgot to update that *lol*. Good catch! You correctly identified and understood the problem, but failed to provide a working solution to the client (the list). It's a good thing that we both have clients who understand we're not prefect. :-) Let's just agree that we both can identify, understand, and solve such minor problems. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
tedd wrote: At 2:48 PM -0400 5/23/10, Robert Cummings wrote: tedd wrote: At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :) Rob: Okay, so I didn't correctly identify and understand the problem from the client (the list), but my solution worked for the problem I understood. Whereas: Doh, what's funny is I fixed it in my test script but had already pasted into my email and forgot to update that *lol*. Good catch! You correctly identified and understood the problem, but failed to provide a working solution to the client (the list). It's a good thing that we both have clients who understand we're not prefect. :-) Let's just agree that we both can identify, understand, and solve such minor problems. If you had been a real client I would have tested before shipping :) My problem would have been caught immediately, yours could have been silently wreaking havoc my altering data. I'd rather a completely broken solution than a half working solution that presents itself at a later time :| The only reason I bothered to bring it up though, is because these miscommunications between client and developer (and any number of in betweens was the focus of a memorable cartoon that our professor presented to all of us in a second year programming course (Prgramming in the Large with C++). It highlighting the conceptual differences between different people in the production chain and how the client pretty mich didn't get what he wanted :) Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
At 2:39 PM -0400 5/24/10, Robert Cummings wrote: -snip- It highlighting the conceptual differences between different people in the production chain and how the client pretty mich didn't get what he wanted :) Cheers, Rob. Rob: Any guy called pretty mich probably doesn't get what he wants anyway. :-) The point is -- we all make mistakes. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. I did not catch there were more than two linefeeds in the OP's problem. Doing more was something I did not address. Also, the solution you provided works better this way: :-) $input = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $input = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $input = trim( $input ); Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
tedd wrote: At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :) I did not catch there were more than two linefeeds in the OP's problem. Doing more was something I did not address. Also, the solution you provided works better this way: :-) $input = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $input = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $input = trim( $input ); Doh, what's funny is I fixed it in my test script but had already pasted into my email and forgot to update that *lol*. Good catch! Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Robert Cummings wrote: tedd wrote: At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :) I did not catch there were more than two linefeeds in the OP's problem. Doing more was something I did not address. Also, the solution you provided works better this way: :-) $input = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $input = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $input = trim( $input ); preg_replace( /(\s)\s+/im, '\\1', $input ); :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Nathan Rixham wrote: Robert Cummings wrote: tedd wrote: At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :) I did not catch there were more than two linefeeds in the OP's problem. Doing more was something I did not address. Also, the solution you provided works better this way: :-) $input = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $input = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $input = trim( $input ); preg_replace( /(\s)\s+/im, '\\1', $input ); :) ahh just read the rest of this thread.. icnase it gets a bit pedantic then here's a horizontal white space only one: preg_replace( /(\h)\h+/im, '\\1', $input ); and vertical only: preg_replace( /(\v)\v+/im, '\\1', $input ); (spot a pattern?) Best, Nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Nathan Rixham wrote: Nathan Rixham wrote: Robert Cummings wrote: tedd wrote: At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :) I did not catch there were more than two linefeeds in the OP's problem. Doing more was something I did not address. Also, the solution you provided works better this way: :-) $input = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $input = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $input = trim( $input ); preg_replace( /(\s)\s+/im, '\\1', $input ); :) ahh just read the rest of this thread.. icnase it gets a bit pedantic then here's a horizontal white space only one: preg_replace( /(\h)\h+/im, '\\1', $input ); and vertical only: preg_replace( /(\v)\v+/im, '\\1', $input ); (spot a pattern?) Hi Nathan, You may want to start testing your solutions. None have worked yet. Not even close :) Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Robert Cummings wrote: Nathan Rixham wrote: Nathan Rixham wrote: Robert Cummings wrote: tedd wrote: At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :) I did not catch there were more than two linefeeds in the OP's problem. Doing more was something I did not address. Also, the solution you provided works better this way: :-) $input = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $input = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $input = trim( $input ); preg_replace( /(\s)\s+/im, '\\1', $input ); :) ahh just read the rest of this thread.. icnase it gets a bit pedantic then here's a horizontal white space only one: preg_replace( /(\h)\h+/im, '\\1', $input ); and vertical only: preg_replace( /(\v)\v+/im, '\\1', $input ); (spot a pattern?) Hi Nathan, You may want to start testing your solutions. None have worked yet. Not even close :) filed under 'works for me' ?php $input = 'blah b asd as d asd a sd da asd d asd da'; echo preg_replace( /(\s)\s+/im, '\\1', $input ); on PHP/5.2.8 produces: blah b asd as d asd a sd da asd d asd da unless I'm completely missing the elephant in the room here! Best, Nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Nathan Rixham wrote: Robert Cummings wrote: tedd wrote: At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :) I did not catch there were more than two linefeeds in the OP's problem. Doing more was something I did not address. Also, the solution you provided works better this way: :-) $input = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $input = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $input = trim( $input ); preg_replace( /(\s)\s+/im, '\\1', $input ); :) ahh just read the rest of this thread.. icnase it gets a bit pedantic then here's a horizontal white space only one: preg_replace( /(\h)\h+/im, '\\1', $input ); and vertical only: preg_replace( /(\v)\v+/im, '\\1', $input ); (spot a pattern?) Hi Nathan, You may want to start testing your solutions. None have worked yet. Not even close :) filed under 'works for me' ?php $input = 'blah b asd as d asd a sd da asd d asd da'; echo preg_replace( /(\s)\s+/im, '\\1', $input ); on PHP/5.2.8 produces: blah b asd as d asd a sd da asd d asd da unless I'm completely missing the elephant in the room here! Doesn't appear to work on the following: $input = ' 1 2 3 4 5 6'; Additionally, your solution modifies lines that weren't asked to be modified :) I realize it potentially makes for a more succinct solution (if you ever get it to work properly in the general case) but it is not a valid solution for the requested functionality-- The OP did not ask for trimming of lines with content ;) Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Nathan Rixham wrote: Robert Cummings wrote: tedd wrote: At 1:02 PM -0400 5/22/10, Robert Cummings wrote: tedd wrote: If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: It's not broken according to my given, which was If that is all (i.e., removing double linefeeds), then this will do it: My code does exactly what was stated. Actually, his comment didn't say double line feeds... his comment said I want THIS to look like THAT. And THIS had a triple line feed and THAT completely normalized it to a single line feed. I realize you misunderstood the problem, but where I work, clients don't think a solution based on incorrect presumptions is a valid solution for a clearly defined problem :) I did not catch there were more than two linefeeds in the OP's problem. Doing more was something I did not address. Also, the solution you provided works better this way: :-) $input = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $input = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $input = trim( $input ); preg_replace( /(\s)\s+/im, '\\1', $input ); :) ahh just read the rest of this thread.. icnase it gets a bit pedantic then here's a horizontal white space only one: preg_replace( /(\h)\h+/im, '\\1', $input ); and vertical only: preg_replace( /(\v)\v+/im, '\\1', $input ); (spot a pattern?) Hi Nathan, You may want to start testing your solutions. None have worked yet. Not even close :) filed under 'works for me' ?php $input = 'blah b asd as d asd a sd da asd d asd da'; echo preg_replace( /(\s)\s+/im, '\\1', $input ); on PHP/5.2.8 produces: blah b asd as d asd a sd da asd d asd da unless I'm completely missing the elephant in the room here! Doesn't appear to work on the following: $input = ' 1 2 3 4 5 6'; Additionally, your solution modifies lines that weren't asked to be modified :) I realize it potentially makes for a more succinct solution (if you ever get it to work properly in the general case) but it is not a valid solution for the requested functionality-- The OP did not ask for trimming of lines with content ;) quote: So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 /quote the above produces what's required; and hence the vertical whitespace only solution included too /(\h)\h+/im what version are you on btw? not being an ass and actually am interested in where it doesn't work (as I use the code on some important sites in some apps that are open sourced) Best, Nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Nathan Rixham wrote: Robert Cummings wrote: You may want to start testing your solutions. None have worked yet. Not even close :) filed under 'works for me' ?php $input = 'blah b asd as d asd a sd da asd d asd da'; echo preg_replace( /(\s)\s+/im, '\\1', $input ); on PHP/5.2.8 produces: blah b asd as d asd a sd da asd d asd da unless I'm completely missing the elephant in the room here! Doesn't appear to work on the following: $input = ' 1 2 3 4 5 6'; Additionally, your solution modifies lines that weren't asked to be modified :) I realize it potentially makes for a more succinct solution (if you ever get it to work properly in the general case) but it is not a valid solution for the requested functionality-- The OP did not ask for trimming of lines with content ;) quote: So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 /quote the above produces what's required; and hence the vertical whitespace only solution included too /(\h)\h+/im what version are you on btw? not being an ass and actually am interested in where it doesn't work (as I use the code on some important sites in some apps that are open sourced) I think I see an issue... either my client, your client, both, or the PHP list is trimming email lines. I have spaces at the end of some of the lines in my example. Let's see if a text file attachment works for this. FYI my command-line is currently running 5.2.11 Cheers, Rob -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. 1 2 3 4 5 6 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Robert Cummings wrote: Nathan Rixham wrote: Robert Cummings wrote: You may want to start testing your solutions. None have worked yet. Not even close :) filed under 'works for me' ?php $input = 'blah b asd as d asd a sd da asd d asd da'; echo preg_replace( /(\s)\s+/im, '\\1', $input ); on PHP/5.2.8 produces: blah b asd as d asd a sd da asd d asd da unless I'm completely missing the elephant in the room here! Doesn't appear to work on the following: $input = ' 1 2 3 4 5 6'; Additionally, your solution modifies lines that weren't asked to be modified :) I realize it potentially makes for a more succinct solution (if you ever get it to work properly in the general case) but it is not a valid solution for the requested functionality-- The OP did not ask for trimming of lines with content ;) quote: So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 /quote the above produces what's required; and hence the vertical whitespace only solution included too /(\h)\h+/im what version are you on btw? not being an ass and actually am interested in where it doesn't work (as I use the code on some important sites in some apps that are open sourced) I think I see an issue... either my client, your client, both, or the PHP list is trimming email lines. I have spaces at the end of some of the lines in my example. Let's see if a text file attachment works for this. FYI my command-line is currently running 5.2.11 Yes it was client stripping out extra whitespace! thanks Rob, replicated your results: 1 2 3 4 5 6 and then 'fixed' to give what's needed: preg_replace( /(((\r|)\n)(\h*|))+/im, '\\1' , $input ); the above keeps line termination the same as in the source file; can you give it a quick check your side to ensure it works (if you don't mind)? modified one that 'cleans' the new lines: preg_replace( /((\r|)\n(\h*|))+/im, PHP_EOL , $input ); Best, Nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Nathan Rixham wrote: Yes it was client stripping out extra whitespace! thanks Rob, replicated your results: 1 2 3 4 5 6 and then 'fixed' to give what's needed: preg_replace( /(((\r|)\n)(\h*|))+/im, '\\1' , $input ); the above keeps line termination the same as in the source file; can you give it a quick check your side to ensure it works (if you don't mind)? No, no, you missed my meaning... I don't care about line ending... I care about trailing whitespace on non-empty lines. modified one that 'cleans' the new lines: preg_replace( /((\r|)\n(\h*|))+/im, PHP_EOL , $input ); Yes, this is preferred with respect to line ending IMHO. But it still strips trailing whitesapce. Almost there... but you've also got that first blank line still hanging around which is supposed to be removed :D Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Robert Cummings wrote: Nathan Rixham wrote: Yes it was client stripping out extra whitespace! thanks Rob, replicated your results: 1 2 3 4 5 6 and then 'fixed' to give what's needed: preg_replace( /(((\r|)\n)(\h*|))+/im, '\\1' , $input ); the above keeps line termination the same as in the source file; can you give it a quick check your side to ensure it works (if you don't mind)? No, no, you missed my meaning... I don't care about line ending... I care about trailing whitespace on non-empty lines. modified one that 'cleans' the new lines: preg_replace( /((\r|)\n(\h*|))+/im, PHP_EOL , $input ); Yes, this is preferred with respect to line ending IMHO. But it still strips trailing whitesapce. Almost there... but you've also got that first blank line still hanging around which is supposed to be removed :D lol ahh hell yeah; trim() it ;) I'm done for the night best, nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Nathan Rixham wrote: Robert Cummings wrote: Nathan Rixham wrote: Yes it was client stripping out extra whitespace! thanks Rob, replicated your results: 1 2 3 4 5 6 and then 'fixed' to give what's needed: preg_replace( /(((\r|)\n)(\h*|))+/im, '\\1' , $input ); the above keeps line termination the same as in the source file; can you give it a quick check your side to ensure it works (if you don't mind)? No, no, you missed my meaning... I don't care about line ending... I care about trailing whitespace on non-empty lines. modified one that 'cleans' the new lines: preg_replace( /((\r|)\n(\h*|))+/im, PHP_EOL , $input ); Yes, this is preferred with respect to line ending IMHO. But it still strips trailing whitesapce. Almost there... but you've also got that first blank line still hanging around which is supposed to be removed :D lol ahh hell yeah; trim() it ;) I'm done for the night *rofl* Have a good night :D Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
At 4:27 PM +0200 5/21/10, Anton Heuschen wrote: So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
tedd wrote: At 4:27 PM +0200 5/21/10, Anton Heuschen wrote: So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
On 5/22/2010 1:02 PM, Robert Cummings wrote: tedd wrote: At 4:27 PM +0200 5/21/10, Anton Heuschen wrote: So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: Your solution doesn't remove the blank lines [\r\n]+ use instead [\r\n]{2,} So 2 or more becomes only 1. In general, problem is trickier when the following are considered. # means any number. 0, 1. some textEOL #spacesEOL more text some text#spacesEOL #spacesEOL any number of these #spacesEOL more text some textEOL EOL ...any number of these EOL some text The white space before the EOLs can also be tabs Look at the solution I posted earlier. The trim() removes all the white spaces -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Al wrote: On 5/22/2010 1:02 PM, Robert Cummings wrote: tedd wrote: At 4:27 PM +0200 5/21/10, Anton Heuschen wrote: So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: Your solution doesn't remove the blank lines [\r\n]+ use instead [\r\n]{2,} So 2 or more becomes only 1. In general, problem is trickier when the following are considered. # means any number. 0, 1. some textEOL #spacesEOL more text some text#spacesEOL #spacesEOL any number of these #spacesEOL more text some textEOL EOL ...any number of these EOL some text The white space before the EOLs can also be tabs Look at the solution I posted earlier. The trim() removes all the white spaces My solution worked well where spaces were not an issue. Your solution breaks my more general solution. Although I did realize I should have trimmed the final output since any empty lead lines will not be removed. Please review and see why you're comment to use [\r\n]{2,} does not work properly. Correcting for lead blank lines and handling spaces in a blank line is also quite simple without having to use the heavy solution of foreach: ?php $data = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $data = trim( $input ); ? Without benchmarking, I'm willing to bet this is faster and less memory intensive than your foreach solution :) Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
On 5/22/2010 4:34 PM, Robert Cummings wrote: Al wrote: On 5/22/2010 1:02 PM, Robert Cummings wrote: tedd wrote: At 4:27 PM +0200 5/21/10, Anton Heuschen wrote: So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: Your solution doesn't remove the blank lines [\r\n]+ use instead [\r\n]{2,} So 2 or more becomes only 1. In general, problem is trickier when the following are considered. # means any number. 0, 1. some textEOL #spacesEOL more text some text#spacesEOL #spacesEOL any number of these #spacesEOL more text some textEOL EOL ...any number of these EOL some text The white space before the EOLs can also be tabs Look at the solution I posted earlier. The trim() removes all the white spaces My solution worked well where spaces were not an issue. Your solution breaks my more general solution. Although I did realize I should have trimmed the final output since any empty lead lines will not be removed. Please review and see why you're comment to use [\r\n]{2,} does not work properly. Correcting for lead blank lines and handling spaces in a blank line is also quite simple without having to use the heavy solution of foreach: ?php $data = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $data = trim( $input ); ? Without benchmarking, I'm willing to bet this is faster and less memory intensive than your foreach solution :) Cheers, Rob. Ignoring the space and tabs, you're right, the + does it. One or more always reduces to one only. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Al wrote: On 5/22/2010 4:34 PM, Robert Cummings wrote: Al wrote: On 5/22/2010 1:02 PM, Robert Cummings wrote: tedd wrote: At 4:27 PM +0200 5/21/10, Anton Heuschen wrote: So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 If that is all (i.e., removing double linefeeds), then this will do it: $text_array = array(); $text_array = explode(\n\n, $input_text); $output_text = implode(\n,$text_array); Sorry tedd, this is broken. It doesn't solve problems with runs of greater than 2 newlines which is even in the example :) I would use the following instead which is also line break agnostic with final output in the style for your system: ?php $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); ? Cheers, Rob. Rob: Your solution doesn't remove the blank lines [\r\n]+ use instead [\r\n]{2,} So 2 or more becomes only 1. In general, problem is trickier when the following are considered. # means any number. 0, 1. some textEOL #spacesEOL more text some text#spacesEOL #spacesEOL any number of these #spacesEOL more text some textEOL EOL ...any number of these EOL some text The white space before the EOLs can also be tabs Look at the solution I posted earlier. The trim() removes all the white spaces My solution worked well where spaces were not an issue. Your solution breaks my more general solution. Although I did realize I should have trimmed the final output since any empty lead lines will not be removed. Please review and see why you're comment to use [\r\n]{2,} does not work properly. Correcting for lead blank lines and handling spaces in a blank line is also quite simple without having to use the heavy solution of foreach: ?php $data = preg_replace( #[\r\n]+[[:space:]]+[\r\n]+#, \n, $input ); $data = preg_replace( #[\r\n]+#, PHP_EOL, $input ); $data = trim( $input ); ? Without benchmarking, I'm willing to bet this is faster and less memory intensive than your foreach solution :) Cheers, Rob. Ignoring the space and tabs, you're right, the + does it. One or more always reduces to one only. But the second solution I provided above addresses all issues (in case you missed that it was another solution :) Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
On Fri, 2010-05-21 at 14:03 +0200, Anton Heuschen wrote: Hi Im trying do something like this, have a function which uploads my file and returns file pointer ... but at same time ... I want to remove all Blank lines in a file and update it before it goes to the final location ... What I tried was to do a write of file and use some regexp replace to remove a blank ... either I am not doing the replace correct or my understanding of the file buffer and what I can do with it between the browser and saving is not correct, Anyway my code looks something like this : $uploadfile = $this-uploaddir; $mtran = mt_rand(999,99); $NewName= date(Ymd_Gis).$mtran..csv; $uploadfile = $uploadfile.$NewName; try{ if (move_uploaded_file($_FILES['attachfile']['tmp_name'], $uploadfile)) { $handle = fopen($uploadfile, r+); $lines = file($uploadfile, FILE_SKIP_EMPTY_LINES); //FILE_IGNORE_NEW_LINES | foreach ($lines as $line_num = $line) { $line = preg_replace(/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/, , $line); if(strlen($line) 0) $line=trim($line); $line=$line.\n; fwrite($handle, $line); } fclose($handle); If the files aren't too large in size, what about using something like file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); which should pull into an array only those lines with content, and then just write that back out to the same file line by line? Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Remove blank lines from a file
On 21 May 2010 15:16, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Fri, 2010-05-21 at 14:03 +0200, Anton Heuschen wrote: Hi Im trying do something like this, have a function which uploads my file and returns file pointer ... but at same time ... I want to remove all Blank lines in a file and update it before it goes to the final location ... What I tried was to do a write of file and use some regexp replace to remove a blank ... either I am not doing the replace correct or my understanding of the file buffer and what I can do with it between the browser and saving is not correct, Anyway my code looks something like this : $uploadfile = $this-uploaddir; $mtran = mt_rand(999,99); $NewName= date(Ymd_Gis).$mtran..csv; $uploadfile = $uploadfile.$NewName; try{ if (move_uploaded_file($_FILES['attachfile']['tmp_name'], $uploadfile)) { $handle = fopen($uploadfile, r+); $lines = file($uploadfile, FILE_SKIP_EMPTY_LINES); //FILE_IGNORE_NEW_LINES | foreach ($lines as $line_num = $line) { $line = preg_replace(/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/, , $line); if(strlen($line) 0) $line=trim($line); $line=$line.\n; fwrite($handle, $line); } fclose($handle); If the files aren't too large in size, what about using something like file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); which should pull into an array only those lines with content, and then just write that back out to the same file line by line? Thanks, Ash http://www.ashleysheridan.co.uk Hi, I actually had that ... removed it in last example as I was trying other stuff and it did not seem to work either ? $lines = file($uploadfile, FILE_SKIP_EMPTY_LINES); //FILE_IGNORE_NEW_LINES | see I removed the FILE_IGNORE line - it was in earlier and only tried FILE_SKIP_EMPTY but still the final file had all the spaces again ... So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 but it never does and still looks like the first block.
RE: [PHP] Remove blank lines from a file
From: Anton Heuschen On 21 May 2010 15:16, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Fri, 2010-05-21 at 14:03 +0200, Anton Heuschen wrote: Hi Im trying do something like this, have a function which uploads my file and returns file pointer ... but at same time ... I want to remove all Blank lines in a file and update it before it goes to the final location ... What I tried was to do a write of file and use some regexp replace to remove a blank ... either I am not doing the replace correct or my understanding of the file buffer and what I can do with it between the browser and saving is not correct, Anyway my code looks something like this : $uploadfile = $this-uploaddir; $mtran = mt_rand(999,99); $NewName= date(Ymd_Gis).$mtran..csv; $uploadfile = $uploadfile.$NewName; try{ if (move_uploaded_file($_FILES['attachfile']['tmp_name'], $uploadfile)) { $handle = fopen($uploadfile, r+); $lines = file($uploadfile, FILE_SKIP_EMPTY_LINES); //FILE_IGNORE_NEW_LINES | foreach ($lines as $line_num = $line) { $line = preg_replace(/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/, , $line); if(strlen($line) 0) $line=trim($line); $line=$line.\n; fwrite($handle, $line); } fclose($handle); If the files aren't too large in size, what about using something like file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); which should pull into an array only those lines with content, and then just write that back out to the same file line by line? I actually had that ... removed it in last example as I was trying other stuff and it did not seem to work either ? $lines = file($uploadfile, FILE_SKIP_EMPTY_LINES); //FILE_IGNORE_NEW_LINES | see I removed the FILE_IGNORE line - it was in earlier and only tried FILE_SKIP_EMPTY but still the final file had all the spaces again ... So in the file it would look like (from the original file the user uploads that is) 1 2 3 4 5 6 but when the file is saved to the server it must look like 1 2 3 4 5 6 but it never does and still looks like the first block. Are those lines actually empty, or do they have other non-printing characters in them? Isn't there a generic whitespace value that could be used in place of '\s\t'? Can you look at the output file with a binary or hex editor to see what is actually in those 'empty' lines? Does that regular expression work correctly on UTF-8 input? Bob McConnell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Remove blank lines from a file
Anton Heuschen writes: Hi Im trying do something like this, have a function which uploads my file and returns file pointer ... but at same time ... I want to remove all Blank lines in a file and update it sed -i '/^$/d' yourFile -- Gary -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php