Remove items from an array that contains only 1 character

4

I have a following array :

$arr = array('estrela', 'não', 'é', 'up','.','Evitem', 'estrelar',
'abobrinha', 'coisa', 'fugaz', 'de', 'interesse', 'praticamente',
'individual', 'conversa mole','.','Só', 'porque', 'é', 'engraçado',
'não','quer','dizer','que','é','importante','e', 'útil', 'para', 
'todos', '.', 'O', 'objetivo', 'do', 'recurso', 'é', 
'destacar', 'algo', 'importante','para','as', 'pessoas', 
'que', 'não', 'são', 'frequentes', 'no', 'chat', 'abusar', 
'dele', 'acaba', 'com', 'sua', 'utilidade', 'Ajam', 'como', 
'comunidade', ',', 'pense', 'no', 'que', 'é', 'realmente', 
'útil', 'para', 'todos', 'Também', 'não' ,'quer', 'dizer', 
'que', 'nada', 'edianamente', 'fútil', 'não', 'pode', 'só', 'sejam', 
'mais', 'seletivos', '.');

I would like to remove all items from array that have only 1 character. For example é , . , O , , , etc; being letter or even some special character like , (comma).

How can I remove items from array that contains only 1 character among them letters and special characters?

    
asked by anonymous 05.03.2017 / 20:36

4 answers

3

You can check the size of the characters in a loop, and remove if the value is a character.

Code:

<?php
    $arr = ["gato", "g", "stack", "ba", "t", "foo", "à", "é", "bar"];

    foreach($arr as $key => $value) {
        if (mb_strlen($value) == 1) {
            unset($arr[$key]);
        }
    }

    print_r($arr);

Output:

Array
(
    [0] => gato
    [2] => stack
    [3] => ba
    [5] => foo
    [8] => bar
)

Output with your array entered as data input:

Array
(
    [0] => estrela
    [1] => não
    [3] => up
    [5] => Evitem
    [6] => estrelar
    [7] => abobrinha
    [8] => coisa
    [9] => fugaz
    [10] => de
    [11] => interesse
    [12] => praticamente
    [13] => individual
    [14] => conversa mole
    [16] => Só
    [17] => porque
    [19] => engraçado
    [20] => não
    [21] => quer
    [22] => dizer
    [23] => que
    [25] => importante
    [27] => útil
    [28] => para
    [29] => todos
    [32] => objetivo
    [33] => do
    [34] => recurso
    [36] => destacar
    [37] => algo
    [38] => importante
    [39] => para
    [40] => as
    [41] => pessoas
    [42] => que
    [43] => não
    [44] => são
    [45] => frequentes
    [46] => no
    [47] => chat
    [48] => abusar
    [49] => dele
    [50] => acaba
    [51] => com
    [52] => sua
    [53] => utilidade
    [54] => Ajam
    [55] => como
    [56] => comunidade
    [58] => pense
    [59] => no
    [60] => que
    [62] => realmente
    [63] => útil
    [64] => para
    [65] => todos
    [66] => Também
    [67] => não
    [68] => quer
    [69] => dizer
    [70] => que
    [71] => nada
    [72] => edianamente
    [73] => fútil
    [74] => não
    [75] => pode
    [76] => só
    [77] => sejam
    [78] => mais
    [79] => seletivos
)

Maybe you have to adapt, because it will depend on the coding you are using, and other things can also influence the results, but it helps a bit you.

See working here .

    
05.03.2017 / 21:38
2

Good afternoon. It is just to iterate the array and check each of the values by the function strlen, that returns the size of the string. If it returns 1, you can remove the element and call it an empty string, or whatever you prefer. In the code I also used the trim () function in case you want to remove strings like "is {white space}"

See the code below:

for ($i = 0; $i < count($arr); $i++) {
if(strlen(trim($arr[$i])) == 1)) {
$arr[$i] = "";

 }
}

I hope you have helped =)

    
05.03.2017 / 20:46
2

You can use the array_filter function.

Example usage:

$filtrado = array_filter($arr, function($item) {
    if(strlen($item) <= 1) {
        return false;
    }
    return true;
});
    
05.03.2017 / 20:58
1

If you're concerned with performance ( "I'm looking for something also in terms of performance." ) you should not use strlen() , use empty() or isset() the difference is small, but it exists.

function remover_palavra_curta(string $string) : bool{
   return isset($string[1]);
}

$arr = array_filter($arr, remover_palavra_curta);

By comparison, I got this result, in order of isset , strlen and mb_strlen .

array(3) {
  [0]=>
  float(0.77282905578613)
  [1]=>
  float(0.81824898719788)
  [2]=>
  float(1.5776748657227)
}

I used this method here for this . The 0 is the isset , the 1 is the strlen and the 2 is the mb_strlen .

The isset and strlen has problem with é for example, since:

$texto = 'é';
var_dump( strlen($texto) === 2 );

Response, as expected :

bool(true)

This also occurs with isset() , $texto[1] also exists. To correct this only by using mb_strlen , or you could use ctype_alpha($texto[1]) , but the performance gain would be completely overridden, the use of mb_strlen being faster.

In order to have whatever you want, you should give up performance and use the slowest, mb_strlen , it removes all é as well as . , as you wish, therefore:

function remover_palavra_curta(string $string) : bool{
   return mb_strlen($string) >= 2;
}
    
05.03.2017 / 23:23