How to scan an array of a variable and compare with another variable?

Asked

Viewed 558 times

0

I have the code below works well, but I would like the information in strings to be taken in an array for example:

$array = array('cia','Cia','dia','Dia','hoje');

and check if one of the above words has in the text variable.

Someone knows how to do it ?

$texto = $_REQUEST['text'];
    if((strpos($texto,'cia')!==false || strpos($texto,'Cia')!==false || strpos($texto,'dia')!==false || strpos($texto,'prod')!==false) || (strpos($texto,'daily')!==false || strpos($texto,'Daily')!==false || strpos($texto,'prod')!==false) || strpos($texto,'hoje')!==false || strpos($texto,'produ%C3%A7%C3%A3o%20di%C3%A1ria')!==false) {

     //pega a pagina que quero redirecionar
}

1 answer

1

Exchange these Ous for the use of function in_array(), it uses two arguments, the first is the string to be searched and the second the items that will be compared.

<?php
$texto = $_REQUEST['text'];
$array = array('cia','Cia','dia','Dia','hoje');

if(in_array($texto, $array)){
   echo 'texto encontrado no array';
}
  • thanks worked

  • If that’s the answer, then yes it’s a duplicate, if he wanted to check if the string had any of the strings in the middle, then it’s a little more complex.

Browser other questions tagged

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