craphunter
craphunter

Reputation: 981

Delete string(1) " "

I get from a from a list:

one     "\n"
        "\n"
two     "\n"

I do an explode when I get the list:

     $list_explode_nl = explode("\n", $list);

var_dump shows:

array(3) { [0]=> string(5) "one " [1]=> string(1) " " [2]=> string(4) "two" }

My code:

  foreach ($list_explode_nl as $k => $v) {
     if ($v == NULL || $v == ' ') {
        unset($list_explode_nl[$k]);
     }
  }

I want to get rid of an element, when the string is like [1]. How?

Upvotes: 0

Views: 52

Answers (2)

Mark Baker
Mark Baker

Reputation: 212452

$list_explode_nl = array_filter(array_map('trim',$list_explode_nl));

or

$list_explode_nl = array_values(array_filter(array_map('trim',$list_explode_nl)));

if you want to reset the indexes as well

Upvotes: 1

drew010
drew010

Reputation: 69967

Try:

foreach ($list_explode_nl as $k => $v) {
    if ($v == NULL || trim($v) == '') {
        unset($list_explode_nl[$k]);
    }
 }

I think the [1] element is a newline (or possibly tab) so it will not equal ' '. trim should take care of it.

Upvotes: 2

Related Questions