How to Remove Blank Lines from Text in PHP

How do I remove blank lines from text in PHP?

// New line is required to split non-blank lines
preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);

The above regular expression says:

/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/
1st Capturing group (^[\r\n]*|[\r\n]+)
1st Alternative: ^[\r\n]*
^ assert position at start of the string
[\r\n]* match a single character present in the list below
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\r matches a carriage return (ASCII 13)
\n matches a fine-feed (newline) character (ASCII 10)
2nd Alternative: [\r\n]+
[\r\n]+ match a single character present in the list below
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\r matches a carriage return (ASCII 13)
\n matches a fine-feed (newline) character (ASCII 10)
[\s\t]* match a single character present in the list below
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\s match any white space character [\r\n\t\f ]
\tTab (ASCII 9)
[\r\n]+ match a single character present in the list below
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\r matches a carriage return (ASCII 13)
\n matches a fine-feed (newline) character (ASCII 10)

How to remove blank line at the end of the text here

Try a different approach:

$new = file('new.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$old = file('old.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$result = array_diff($new, $old);
file_put_contents('Output.txt', implode(PHP_EOL, $result));
  • Read the files into an array with file() stripping the newlines FILE_IGNORE_NEW_LINES and skipping empty lines FILE_SKIP_EMPTY_LINES
  • Run array_diff() on the arrays
  • implode() the result array on a newline and write it to the output file

PHP Simple way to replace or remove empty lines with str_replace

You shouldn't use - in variable names ;)

$line_out = preg_replace('/[\n\r]+/', '', $line_in);
$line_out = str_replace(array("\n", "\r"), '', $line_in);

Manual entries:

  • http://php.net/manual/en/function.preg-replace.php
  • http://php.net/manual/en/function.str-replace.php

Remove blank lines from a text file

If the file is not too large:

file_put_contents('newFile.txt',
implode('', file('tagged.txt', FILE_SKIP_EMPTY_LINES)));

Regex remove blank lines, but leave new line

I think this is what you want: ^\s+


Use:

$string = preg_replace('/^\s+/m', '', $string);

Explanation:

  • ^ anchors to the beginning of a new line with the m multi-line modifier
  • \s+ finds 1+ whitespace ([\r\n\t\f ]) character; this could be an entire blank line or just preceding indents

Removing blank lines from a textarea's output

Using regex to eliminate blank lines before exploding (works well for any number of consecutive blank lines, also see next snippet):

$text = preg_replace('/\n+/', "\n", trim($_POST['textarea']));

Splitting with a regex:

$lines = preg_split('/\n+/', trim($_POST['textarea']));
$text = implode("\n", $lines);

Splitting without a regex:

$lines = array_filter(explode("\n", trim($_POST['textarea'])));
$text = implode("\n", $lines);

Just feeling a tad creative today, pick your poison :)

remove blank lines at the beginning

Should work better:

$content = ltrim($content);


Related Topics



Leave a reply



Submit