Search code examples
phppreg-replacestrpos

Contains em PHP in return Regex


I am having difficulty in searching for a specific word in text.

I tried to use the stripos method, but it throws an error because the text return is an object:

$text = preg_replace("/note:/" ,'' , $text);

if(stripos($text, "note:") !== false){
   return true
}

I need it to return if the string contains that word inside of the text to use in my if.


Solution

  • Maybe, we don't need a preg_replace and strpos would be enough:

    $text = 'some word before then note: and some words after';
    $word = "note:";
    
    if (strpos($text, $word) !== false) {
        echo "YAAAY {$word} found in position: " . strpos($text, $word);
    }
    

    Output

    YAAAY note: found in position: 22