Searching word within text of PHP Array

Asked

Viewed 959 times

2

have an array with words inside, separated by ; and need to locate certain word to carry out unset on it.

$element = ';nao;';
$text = array('conjunto;palavras;nao','palavras;sim','nao;texto','texto;dividido;nao');

$key = array_search($element, $text);
if($key!==false){
    unset($text[$key]);
}

I used the above code, but it can only locate if the word is alone without the others inside the array.

  • For this example would all values that have the "no" be excluded? That is, the result would be ['palavras;sim']?

  • Good afternoon. What do you need this for?

  • The ones that should remain are the ones that have ;yes; inside the array. I need to clean the pdf I get from Tran and turn into CSV, but I only need the arrays that have yes to update the database.

2 answers

3


First tip is to avoid the use of the unset unless necessary, as you lose the original value of your variable and this can complicate the debugging process during maintenance - you cannot compare input/output because you modify one to get the other. What you want to do is filter the values of array, so we already have a function to use: array_filter. Each value of array is a set of words separated by semicolons, so we have to separate them into a list of words:

$result = array_filter($text, function ($setence) use ($element) {
    return !in_array($element, explode(';', $setence));
});

See working on Repl.it | Ideone

With that, the result would be:

Array
(
    [1] => palavras;sim
)

But when the word is sought "nao", without the characters around the word, which should not belong to it since they are separators.

If you want to avoid memory allocation that function explode intrinsically will do to store the substrings in memory, you can utilize the strpos (or mb_strpos if working with multi bytes) treating the situations where the word can start or end with the searched word by adding the separator character before and after the text:

$result = array_filter($text, function ($setence) use ($element) {
    return strpos(";{$setence};", ";{$element};") === false;
});

Producing the same result.

  • Actually unset left me holes in the list of arrays, so with strpos it worked, I made a foreach picking array by array and using strpos, the ones that didn’t; I built an array with them.

-1

$element = ';nao;';
$text = array('conjunto;palavras;nao','palavras;sim','nao;texto','texto;dividido;nao');

foreach ($text as $key => $value) {
    $pos = strpos($value, $element);

    if ($pos !== false) {
        unset($text[$key]);
    }
}

Your search ";nao;" will not find any results because in this your array does not have a no with ; in the beginning and end, then look for ";no" or "no";".

Or modify the example to accept both

  • 1

    Forcing the search by adding the separator character to the string makes no sense and generates side effects. Searching for nao; can remove items that have words ending in nao wrongfully, such as anao;sim; in the same way as seeking to ;nao would remove terms initiated in nao, as sim;naomi

Browser other questions tagged

You are not signed in. Login or sign up in order to post.