Detecting Keyword

Asked

Viewed 55 times

1

I’m trying to make a keyword detection system.

Type: "asdfghjlzzTESTEzzxcvbnm"

And I have to put in the condition, to see if there is or not the X word

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

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

1 answer

4


You can use the function 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.";
}

Demonstration

Link to documentation

Or the function stripos

$String = "asdfghjlzzTESTEzzxcvbnm";

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

Demonstration

Link to documentation

  • Thanks, it worked right....

Browser other questions tagged

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