从PHP数组中删除空格/空值

时间:2022-03-05 19:20:16

I'm developing a word counting application and I'm using following code to load words to an array.

我正在开发一个单词计数应用程序,并使用以下代码将单词加载到数组中。

$str = ' Some string is here     ...  Another string.. ';
$str = trim($str);
$str = preg_replace('/\s+/', ' ', $str);
$str_array = explode(" ", $str);
$str_array = array_filter($str_array);

But the issue is I'm getting empty values in my array like follows.

但问题是我在数组中得到的是空值,如下所示。

Array
(
    [0] => Word1
    [1] => Word2
    [2] => Word3
    [3] => 
    [4] => 
    [5] => 
)

I've tried array_filter($str_array) and more * answers. But I was failed to get this done. Can some one please help me to fix this issue? Thanks!

我尝试了array_filter($str_array)和更多*的答案。但我没能完成。有人能帮我解决这个问题吗?谢谢!

4 个解决方案

#1


4  

Trim the array elements using trim by array_map and finally do an array_filter using strlen as the call-back.

通过array_map使用Trim来修剪数组元素,最后使用strlen作为回调使用array_filter。

<?php
$arr=Array
(
    0 => 'Word1',
    1 => 'Word2',
    2 => 'Word3',
    3 => ' ',
    4 => '',
    5 => ''
);

$new_arr = array_filter(array_map('trim',$arr),'strlen');
print_r($new_arr);

Demo

演示

#2


1  

Try this...

试试这个…

 foreach ($array as $key=>$value) 
 { 
     if($value == '')
     {
       unset($array[$key]);
     }
 }

#3


0  

$str = ' Some string is here     ...  Another string.. ';
$str = trim($str);
$str = str_replace('  ', ' ', $str); //replace two space to single space
$str_array = explode(" ", $str);

#4


0  

I was able to fix this issue with $str = preg_replace('/[^(\x20-\x7F)]*/','', $str);

我能够解决这个问题$ str = preg_replace(' /[^(\ x20 - \ x7F)]* /”,”str美元);

Source

        $str = strip_tags($str);
        $str = trim($str);
        $str = preg_replace('/ HYPERLINK "[^"]*" /', '', $str);
        $str = preg_replace('/\s+/', ' ', $str);
        $str = preg_replace('/[^(\x20-\x7F)]*/','', $str);

#1


4  

Trim the array elements using trim by array_map and finally do an array_filter using strlen as the call-back.

通过array_map使用Trim来修剪数组元素,最后使用strlen作为回调使用array_filter。

<?php
$arr=Array
(
    0 => 'Word1',
    1 => 'Word2',
    2 => 'Word3',
    3 => ' ',
    4 => '',
    5 => ''
);

$new_arr = array_filter(array_map('trim',$arr),'strlen');
print_r($new_arr);

Demo

演示

#2


1  

Try this...

试试这个…

 foreach ($array as $key=>$value) 
 { 
     if($value == '')
     {
       unset($array[$key]);
     }
 }

#3


0  

$str = ' Some string is here     ...  Another string.. ';
$str = trim($str);
$str = str_replace('  ', ' ', $str); //replace two space to single space
$str_array = explode(" ", $str);

#4


0  

I was able to fix this issue with $str = preg_replace('/[^(\x20-\x7F)]*/','', $str);

我能够解决这个问题$ str = preg_replace(' /[^(\ x20 - \ x7F)]* /”,”str美元);

Source

        $str = strip_tags($str);
        $str = trim($str);
        $str = preg_replace('/ HYPERLINK "[^"]*" /', '', $str);
        $str = preg_replace('/\s+/', ' ', $str);
        $str = preg_replace('/[^(\x20-\x7F)]*/','', $str);