我怎样才能剥离空白,从一个数组元素只? [英] How can I strip whitespace only elements from an array?

查看:79
本文介绍了我怎样才能剥离空白,从一个数组元素只?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样可以删除包含只是空白的数组中的所有元素,的的空白中的一个元素,比如foobar的只是空数组像元素

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 " "?

感谢。

推荐答案

preg_grep() 是你的朋友。

preg_grep() is your friend.

$array = array("This", " ", "is", " ", "a", " ", "test.");

$array = preg_grep('/^\s*\z/', $array, PREG_GREP_INVERT);

var_dump($array);

codePAD

这将放弃所有阵列成员,其中的字符串为空或仅根据 \\ S组成的空白字符类(空格,制表符和换行符)。

This will drop all array members of which the string is blank or only consist of whitespace according to \s character class (spaces, tabs, and line breaks).

array(4) {
  [0]=>
  string(4) "This"
  [2]=>
  string(2) "is"
  [4]=>
  string(1) "a"
  [6]=>
  string(5) "test."
}

这篇关于我怎样才能剥离空白,从一个数组元素只?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆