2

I was just wondering how I could exactly remove white space or even character by row and position inside row. I can't find info about this but I think that the PHP trim() could help. Also I was thinking about a solution for dynamic text. Where I want do delete all line's which are empty. Or just some of them.

An example:

> here is text blabla
> 
> here is text balabla  balabala
> 
1
2 
3 
> oops too much space
> 
> also here is text

Now how can I strip the 3 white spaces away?

It has to become something like this:

> here is text blabla
> 
> here is text balabla  balabala
> 
> oops too much space
> 
> also here is text
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
botenvouwer
  • 4,334
  • 9
  • 46
  • 75

3 Answers3

4

Seems like you want regular expressions then:

preg_replace("#(\r?\n){3,}#", "\n\n", $str);

It replaces 3 newlines or more (i.e. two consecutive empty lines or more) with two newlines (one empty line).

To make sure it works with lines that only seem empty (i.e. a line with only spaces), you need to alter the expression slightly:

preg_replace("#(\r?\n\s*){3,}#", "\\1\\1", $str);

Disclaimer: The \\1\\1 idea came from NullPointerException :)

You can also take a different route and solve it iteratively:

// $final is the result of the operation
// $n keeps track of how many empty lines were seen
$final = ''; $n = 0;
// $str is the original content, we split it into separate lines
foreach (preg_split("/\r?\n/", $str) as $line) {
        if (strlen(trim($line))) {
                $n = 0;
        } elseif (++$n >= 2) {
                continue;
        }
        // append to the final result
        $final .= "$line\n";
}
// rtrim($final, "\n");

Turns out that using explode() in the above iterative solution improves the performance; it still works with space-only lines because of trim(). However, you need to trim the newlines on the right side using rtrim($final, "\n");.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • @NullUserException Haha figured you'd see that lol. I was distracted while I wrote the first pass. – Ja͢ck Oct 10 '12 at 08:30
  • can we haz an speedtest about what is faster? – EaterOfCode Oct 10 '12 at 08:51
  • @Jack I just did wrote a speedtest http://codepad.viper-7.com/o9jAch mine is faster on codepad but yours is faster on my WAMP :O – EaterOfCode Oct 10 '12 at 09:28
  • @EaterOfCorpses The difference is mainly due to `explode()` vs `preg_replace()`. Both our iterative solutions don't return the same result as the `preg_replace()` though; yours adds a newline in front, mine one at the back :) – Ja͢ck Oct 10 '12 at 09:42
  • Works like a charm, you did it. Stil I am very confused by all the weird parameters inside the usage of preg_replace(). Maby I have to study them more!? – botenvouwer Oct 10 '12 at 09:47
  • @Jack but why is explode faster on codepad and preg on WAMP :S – EaterOfCode Oct 10 '12 at 09:48
1

not regex solution:

 function stripEmptyNewlines($string,$limit = 1){
     $array = explode("\n",$string);
     $emptyLine = 0;
     $newString = "";
     foreach($array as $child){
         if(trim($child) == "") {
             $emptyLine++;
         } else {
             $emptyLine = 0;
         }
         if($emptyLine < $limit + 1){
             $newString .= "\n" . $child;
         }
     }
     return $newString;
 }
EaterOfCode
  • 2,202
  • 3
  • 20
  • 33
-1

use the ReadLine() method on your text.

if ReadLine().Trim() != "", then add it to the output string. (else it will be an empty line)

voluminat0
  • 866
  • 2
  • 11
  • 21