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.

2
  • Hint: use empty() function whenever evaluating empty values. For instance, if(($arr[$i]) != '') could be if(!empty($arr[$i])). Commented May 19, 2015 at 12:39
  • Have you tried the solution to this question? stackoverflow.com/questions/5710174/… Commented May 19, 2015 at 12:52

3 Answers 3

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

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

Sign up to request clarification or add additional context in comments.

5 Comments

nl2br() puts an <br> 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 <br> in it!!
Tip: print htmlentities(var_export($arr,true)); so you will see the <br> in your array
You shouldn't necessarily have to use htmlentities with the var_dump as it will display the entire contents of the variable, including the <br />.
That was the plan. See the <br> stuff on page, to get it right. @the_pete
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.
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.

1 Comment

Thanks pete for your kind consideration
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>";

}?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.