August 20th, 2008, 11:32 AM
Remove empty lines ?
I have this lines:
Between "text line1" and "text line2" exist 2 blank line. Between "Text line2" and "text line3" exist 5 blank lines.
Is possible to specify using a regexp pattern the following : ?
If more than 1 blank line exist in document remove all other blank line, and leave only one. Something like:
August 20th, 2008, 12:26 PM
Sure it's possible. However, Dev Shed is not a free program-writing service so you're unlikely to have somebody just give it to you.
Why don't you go through how you're thinking of approaching it for us and we can assist from there?
August 20th, 2008, 12:34 PM
I need a pattern and not a program!
August 20th, 2008, 10:54 PM
you need to adjust your attitude if you want to get help.
August 21st, 2008, 03:43 AM
working on Solaris[5-9], preferred languages french and C.
August 22nd, 2008, 12:32 PM
If you see, I don't ask for a complete application. This solution work great:
Now I have something like this:
preg_replace("!^\s+(\D)!m", "\\1", $text);
I don't understand how to check if a blank line exist before any number (for example: 1 or 2). If blank line not exist put a blank line. Finnaly I need this output:
I read the whole preg_match documentation but I don't understand how to solve this issue.
August 22nd, 2008, 02:34 PM
This may be a case where carrying the full load via regex is difficult and unnecessary. This seems to work:
$fp_in = fopen("input.txt", "r");
$fp_out = fopen("output.txt", "w");
$previous_line = FALSE;
$current_line = "";
// Read file line-by-line
while (FALSE !== ($current_line = fgets($fp_in)))
// Check for leading digit
if (0 != preg_match("/^\d.*/", $current_line))
// Check if previous_line was not blank
if (FALSE !== $previous_line &&
"\n" != $previous_line)
// Need to add a blank line
$previous_line = $current_line;
I ♥ ManiacDan & requinix
This is a sig, and not necessarily
a comment on the OP:
don't be a help vampire
August 25th, 2008, 06:24 AM
Even with that solution, you're still using a regexp to see if the line starts with a digit.
Note this works in Perl and is untested in PHP. I'm not entirely familiar with the difference between their regexp engines.
... with this: