How can I strip whitespace only elements from an array?

前端 未结 5 1326
有刺的猬
有刺的猬 2021-01-23 05:54

How I can remove all elements of an array that contain just whitespace, not whitespace in an element like \"foobar \" but just empty array elements like <

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-23 06:16

    $arr = array("This", " ", "is", " ", "a", " ", "test.");
    $result = array();
    for($arr as $x) {
         if(!preg_match("/^\s*$/", $x)) $result[] = $x;
    }
    $arr = $result;
    

提交回复
热议问题