How to check if a snippet of a string is in an array item

Asked

Viewed 1,413 times

2

Does anyone know how to check if a reference or string is in an array? I tried this method and it didn’t work

$array = "facebook.com, google.com, twitter.com";
$ref = "http://m.facebook.com";
$arrayIds = explode(',', $array);

if(isset($ref) && (strstr($ref,  $arrayIds) !== false)) {
    echo "sim: ";
} else {
    echo "não";
}

Thank you

  • Two things: 1 - That the array is actually not an array, but a string. The correct writing of the array would be $array = array("facebook.com", "google.com", "twitter.com"); 2 - That the informed reference also does not exist in the "Array".

  • Right @Adrianoluz , but it has to be that way? $array = "facebook.com, google.com, twitter.com"; because it’s in the database, I just gave an example there of how it’s in the table.

  • power even can, if it is an example or exercise. But you need to understand that your bank is not respecting the Normal Way 1

  • @Fernandooliveira took the liberty of correcting the title. It was leading to the wrong answers.

3 answers

3


In your example the function strstrdid not work because it works to return an excerpt of a string contained in another, not valid for array.

So what to do?

If the need is to check whether within the array $arrayIds there is a string that contains part of another, I suggest you combine array_map with the desired function. Then use in_ array to see if there is any true occurrence for this function.

An example, I want to know if "bola" exists somewhere in the strings that are inside the array.

Behold:

 $produtos = array(
     'bola vermelha',
     'boneca Barbie',
     'caneca de ouro'
 );

 // Essa função será executada em cada item do array acima

 $verifica_string = function ($value)
 {
       return strpos($value, 'bola') !== false;
 };

 $lista_de_verificacoes = array_map($verifica_string, $produtos);

 var_dump(in_array(true, $lista_de_verificacoes , true)); // Retornará "true" se "bola" existir no array

You can still optionally use the function preg_grep followed by a count to know if any reference has been found. I believe you will need to use less code in this case, but you will need to use regular expression:

   // Temos que escapar os valores por causa do "."

   $refRegex = sprintf('/%s/', preg_quote('facebook.com'));

   if (count(preg_grep($refRegex, $arrayIds))) > 0) {
     // tem a string
   }

There is also a third way, which is using the function array_filter. But in this case, when the situation is complex, as shown above, I always like to leave a function ready for this, in case it is necessary to reuse:

 function array_contains_value($neddle, array $array)
 {

     return (boolean) count(array_filter($array, function ($value) use($neddle)
     {
          return strpos($value, $neddle) !== false;
     }));
 }

In the above function I use the following functions:

  • count - Counts the values of array or a class that implements Countable

  • array_filter - Removes the values of array according to the callback. Will be removed if return FALSE

  • strpos - Checks if the string contains the specified snippet.

You could check like this:

 array_contains_value('facebook.com', ['www.facebook.com', 'www.google.com']);
  • Thank you for commenting. I already tried to make this method but I wanted to check if there is a piece of string in the reference, because in the case the reference is http://m.facebook.com and the string to check is facebook.com.

  • Ah, yes. It’s simple. I’ll edit the question

  • 1

    Remember: If you solved the problem, don’t forget to leave +1 or mark the question as Util

  • Thank you again, your code is complex but I understood a little, I managed to do it your way, thank you very much.

0

A simpler example, I believe it answers you.

$array = "facebook.com, google.com, twitter.com";
$ref = "http://m.facebook.com";
$arrayIds = explode(',', $array);

if (in_array($ref,$arrayIds)){
    echo 'Existe';
}else{
    echo 'Não existe';
}
  • 1

    Disregard, I read only the statement of the question. I saw in the comments that it was not what I really needed

-2

Let’s do the opposite, let’s check if one of the entries of the array exists in the reference variable, so any reference that contains facebook.com will be valid

$array = "facebook.com, google.com, twitter.com";
$array = explode(trim(","),$array);
$ref = "http://m.facebook.com";
$flag = false;

foreach($array as $a){
if (preg_match("/{$a}/",$ref)){
       $flag = true;
   }
}

if($flag){
    echo 'sim';
}else{
    echo 'nao';
}
  • Your code has a serious problem. o Character . in regular expression means all. You need to escape it. Otherwise, your code will cause a negative effect, causing problems to AP

Browser other questions tagged

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