1

I want to avoid any empty string in an array or any kind of white space. I'm using the following code:

<?php
$str="category 1
      category 2


      category 3

      category 4

      ";
$var = nl2br($str);

echo $var."<br>";
$arr = explode("\n", $var);

var_dump($arr); echo "<br>";
for($i = 0; $i < count($arr); $i++) {

    if(($arr[$i]) != '')
    {
        echo $arr[$i]."</br>";      
    }
}
?>

How can I remove the white space or empty string like the 2,3,5 index in the array which don't have needed string.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
  • Hint: use [`empty()`](http://php.net/manual/en/function.empty.php) function whenever evaluating empty values. For instance, `if(($arr[$i]) != '')` could be `if(!empty($arr[$i]))`. – mathielo May 19 '15 at 12:39
  • Have you tried the solution to this question? http://stackoverflow.com/questions/5710174/how-can-i-strip-whitespace-only-elements-from-an-array – Dipen Shah May 19 '15 at 12:52

3 Answers3

1
$array = array_filter(array_map('trim',$array)); 

Removes all empty values and removes everywhere spaces before and after content!

JustOnUnderMillions
  • 3,741
  • 9
  • 12
  • 1
    nl2br() puts an
    tag after all \n , keep that in mind. function should called br2nl() instead!! So if you do this `$arr = explode("\n", $var);` after `$var = nl2br($str);` you may have entrys with only
    in it!!
    – JustOnUnderMillions May 19 '15 at 12:49
  • Tip: print htmlentities(var_export($arr,true)); so you will see the
    in your array
    – JustOnUnderMillions May 19 '15 at 12:54
  • You shouldn't necessarily have to use `htmlentities` with the `var_dump` as it will display the entire contents of the variable, including the `
    `.
    – the_pete May 19 '15 at 13:02
  • That was the plan. See the
    stuff on page, to get it right. @the_pete
    – JustOnUnderMillions May 19 '15 at 13:50
  • I know, just saying that you shouldn't need to use additional processing when the `var_dump` accomplishes the same thing you suggested in your comment. – the_pete May 19 '15 at 14:58
0

Your code was pretty much correct, but as was pointed out, your nl2br was adding a <br> to the end of each line and thus making this a harder process than it should have been.

$str="category 1
category 2


category 3

category 4

";
$arr = explode("\n", $str);
var_dump($arr); echo "<br>";
    for($i = 0; $i < count($arr); $i++){
    if(($arr[$i]) != '')
    {
        echo $arr[$i]."</br>";
    }
}

I used your old code and just removed the nl2br line as well as initial echo and from there onward, your code actually accomplishes your goal because the explode("\n", $str) is all that you need to split on empty lines and your if statement covers any lines that happen to be only whitespace.

the_pete
  • 822
  • 7
  • 19
0

Finally I've solved it using the following code. Thanks everyone for you help.

<?php
$str="category 1
category 2


category 3

category 4




";
$arr = explode("\n", $str);
var_dump($arr);
$result = array();
for($i=0;$i<count($arr);$i++) {
     if(!preg_match("/^\s*$/", $arr[$i])) $result[] = $arr[$i];
}
$arr = $result;
var_dump($arr);

    for($i = 0; $i < count($arr); $i++){

        echo $arr[$i]."</br>";

}?>