Pseudo Code
text = "I go to school";
word = "to"
if ( word.exist(text) ) {
return true ;
else {
return false ;
}
I am looking for a PHP function which returns true if the word exists in the text.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have a few options depending on your needs. For this simple example,
strpos()is probably the simplest and most direct function to use. If you need to do something with the result, you may preferstrstr()orpreg_match(). If you need to use a complex pattern instead of a string as your needle, you’ll wantpreg_match().strpos() and stripos() method (stripos() is case insensitive):
strstr() and stristr() method (stristr is case insensitive):
preg_match method (regular expressions, much more flexible but runs slower):
Because you asked for a complete function, this is how you’d put that together (with default values for needle and haystack):
PHP 8.0.0 now contains a str_contains function that works like so: