Detecting Keyword

1

I'm trying to make a system that detects a keyword.

Type: "asdfghjlzzTESTEzzxcvbnm"

And I have to by in the condition, to see if it has or not the word X

if($palavraChave == "TESTE"){
  echo "exist";
}else{
  echo "not exist";
}

My problem is how I will be detecting the word 'key' even if there is no space between them.

    
asked by anonymous 31.08.2017 / 05:02

1 answer

4

You can use the preg_match

$String = "asdfghjlzzTESTEzzxcvbnm";

// Verifica se a variavél $String contém a palavra TESTE
if (preg_match("/TESTE/i", $String)) {
    echo "Uma correspondência foi encontrada.";
} else {
    echo "Não foi encontrada uma correspondência.";
}

Demo

Documentation link

Or the function stripos

$String = "asdfghjlzzTESTEzzxcvbnm";

if (stripos($String, 'TESTE') !== false) {
    echo "Existe";
} else {
    echo "Não existe";
}

Demo

Documentation link

31.08.2017 / 05:36