WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES
Advertisements

How to Check If a String Contains a Specific Word in PHP

Topic: JavaScript / jQueryPrev|Next

Answer: Use the PHP strpos() Function

You can use the PHP strpos() function to check whether a string contains a specific word or not.

The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false. Also note that string positions start at 0, and not 1.

  • <?php
  • $word = "fox";
  • $mystring = "The quick brown fox jumps over the lazy dog";
  •  
  • // Test if string contains the word 
  • if(strpos($mystring, $word) !== false){
  •     echo "Word Found!";
  • } else{
  •     echo "Word Not Found!";
  • }
  • ?>

Related FAQ

Here are some more FAQ related to this topic:

Advertisements

Your Feedback:

We would love to hear from you, please drop us a line.