{"id":87,"date":"2019-09-16T16:13:40","date_gmt":"2019-09-16T16:13:40","guid":{"rendered":"https:\/\/tutorialio.com\/?p=87"},"modified":"2025-11-04T10:32:00","modified_gmt":"2025-11-04T10:32:00","slug":"check-if-string-contains-a-specific-word-or-substring","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/check-if-string-contains-a-specific-word-or-substring\/","title":{"rendered":"How Can I Check if String Contains a Specific Word or Substring?"},"content":{"rendered":"<p>Let\u2019s say you have a string and you want to check if it contains a specific word, character or substring. There are a lot of things that you need to be aware of in order to do the check properly without making silly mistakes. For example, using <code>!=<\/code> instead of <code>!==<\/code> during the check can sometimes result in false negatives. Similarly, you might be looking for a word in a case-insensitive manner but forget that the method you used is case-sensitive.<\/p>\n<p>In this tutorial, you will learn about different methods to check if a particular word, character or substring exists in another string. We will also discuss the pros and cons of each of these methods in detail.<\/p>\n<h2 id=\"using-strpos-to-check-if-string-contains-substring-case-sensitive\">Using strpos() to Check if String Contains Substring [Case Sensitive]<\/h2>\n<p>The easiest way to check if a string contains a specific word is with the help of <a href=\"..\/string\/strpos-function.php\">PHP strpos() function<\/a>. PHP <code>strpos()<\/code> function returns the position of the first occurrence of a substring in a string. It returns FALSE if the word or substring was not found. This means that you can compare the return value of <code>strpos()<\/code> to FALSE to check for a substring. The following example will show it in action.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$the_string = \"I am 5 years older than you.\";\n$the_word  = \"years\";\n$the_character = \"I\";\n$the_substring = \"5 years\";\n\n\/\/ Output \u2014 The word \"years\" exists in given string.\nif (strpos($the_string, $the_word) !== false) {\n  echo 'The word \"'.$the_word.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The character \"I\" exists in given string.\nif (strpos($the_string, $the_character) !== false) {\n  echo 'The character \"'.$the_character.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The substring \"5 years\" exists in given string.\nif (strpos($the_string, $the_substring) !== false) {\n  echo 'The substring \"'.$the_substring.'\" exists in given string.';\n}\n<\/code><\/pre>\n<p>You should note that I have used the strict inequality operator (<code>!==<\/code>). If the word we are looking for occurs at the beginning of the string, <code>strpos()<\/code> will return 0 which will evaluate to FALSE value with <code>!=<\/code> operator.  Here is an example:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">\/\/ Output \u2014 The character \"I\" does not exist in given string.\nif (strpos($the_string, $the_character) != false) {\n  echo 'The character \"'.$the_character.'\" exists in given string.';\n} else {\n  echo 'The character \"'.$the_character.'\" does not exist in given string.';\n}<\/code><\/pre>\n<p>Another thing to keep in mind is that while you might be looking for an exact word match like \u201cam\u201d, the function will also return TRUE if the string contains words like \u201csam\u201d, \u201cpam\u201d or \u201camazing\u201d. This may or may not be what you want to achieve. I just wanted to point it out so that you can proceed with caution.<\/p>\n<p>You can also use <code>&gt; -1<\/code> instead of <code>!==<\/code> because even if <code>strpos()<\/code> returns 0 as index value, it will still be greater than -1. However, remember that the greater than operator (<code>&gt;<\/code>) is slower than the strict inequality operator (<code>!==<\/code>).<\/p>\n<h2 id=\"using-stripos-to-check-if-string-contains-substring-case-insensitive\">Using stripos() to Check if String Contains Substring [Case Insensitive]<\/h2>\n<p>If you are looking for a word or substring inside another string but don\u2019t care about the case of the matched substring, you can use the <a href=\"..\/string\/stripos-function.php\">stripos() function<\/a> in PHP. This function is similar to the <code>strpos()<\/code> function we discussed in the previous section. The only difference is that it ignores the case when looking for a substring inside another string.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$the_string = \"Ben likes both apples and oranges.\";\n$the_word  = \"ben\";\n$the_character = \"I\";\n$the_substring = \"LiKes BoTH\";\n\n\/\/ Output \u2014 The word \"ben\" exists in given string.\nif (stripos($the_string, $the_word) !== false) {\n  echo 'The word \"'.$the_word.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The character \"I\" exists in given string.\nif (stripos($the_string, $the_character) !== false) {\n  echo 'The character \"'.$the_character.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The substring \"LiKes BoTH\" exists in given string.\nif (stripos($the_string, $the_substring) !== false) {\n  echo 'The substring \"'.$the_substring.'\" exists in given string.';\n}\n<\/code><\/pre>\n<p>The <code>strpos()<\/code> function would have returned FALSE in all the cases above but <code>stripos()<\/code> ignored the case and returned TRUE.<\/p>\n<p>Another way to check for a case-insensitive match would be to first convert all the strings and substrings that you want to compare to the same case using either <a href=\"..\/string\/strtolower-function.php\">strtolower()<\/a> or <a href=\"..\/string\/strtolower-function.php\">strtoupper()<\/a> function. After that you could just use the <code>strpos()<\/code> function to perform the check. However, it is simply easier to use the <code>stripos()<\/code> function. <\/p>\n<h2 id=\"using-regex-to-check-if-string-contains-substring\">Using Regex to Check if String Contains Substring<\/h2>\n<p>Another option to see if there is a word, character or substring in a string is to use regular expressions. They are generally better suited when you are looking for more complex search patterns in a string.<\/p>\n<p>In situations where you are searching for a particular word, using regular expression might not be a great idea. Keep in mind that using simple <code>strpos()<\/code> is about 3 times faster than using regular expressions. Anyway, the following code snippet will show you how to look for a word, character or substring in a string using regular expressions:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$the_string = \"I am 5 years older than you.\";\n$the_word  = \"years\";\n$the_character = \"I\";\n$the_substring = \"5 years\";\n\n\/\/ Output \u2014 The word \"years\" exists in given string.\nif (preg_match('\/years\/', $the_string)) {\n  echo 'The word \"'.$the_word.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The character \"I\" exists in given string.\nif (preg_match('\/I\/', $the_string)) {\n  echo 'The character \"'.$the_character.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The substring \"5 years\" exists in given string.\nif (preg_match('\/5 years\/', $the_string)) {\n  echo 'The substring \"'.$the_substring.'\" exists in given string.';\n}\n<\/code><\/pre>\n<p>As I mentioned earlier, using <a href=\"http:\/\/php.net\/manual\/en\/function.preg-match.php\">preg_match()<\/a> only makes sense when you searching for more complicated patterns instead of simple words or substrings. For instance, you can use this function to check if a substring contains any words with 10 or more characters etc. Here is a basic example:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$the_string = 'Photosynthesis and jeopardize are big words.';\n\n\/\/ Output \u2014 The given string contains words with 10 or more letters.\nif (preg_match('\/\\w{10,}\/i', $the_string)) {\n  echo 'The given string contains words with 10 or more letters.';\n}\n<\/code><\/pre>\n<p>You can make all the substring checks which use regex, case-insensitive by adding the flag <code>i<\/code> at the end of the pattern. The following example will make it more clear:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$the_string = \"Ben likes both apples and oranges.\";\n$the_word  = \"ben\";\n$the_character = \"I\";\n$the_substring = \"LiKes BoTH\";\n\n\/\/ Output \u2014 The word \"years\" exists in given string.\nif (preg_match('\/ben\/i', $the_string)) {\n  echo 'The word \"'.$the_word.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The character \"I\" exists in given string.\nif (preg_match('\/I\/i', $the_string)) {\n  echo 'The character \"'.$the_character.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The substring \"5 years\" exists in given string.\nif (preg_match('\/LiKes BoTH\/i', $the_string)) {\n  echo 'The substring \"'.$the_substring.'\" exists in given string.';\n}\n<\/code><\/pre>\n<h2 id=\"using-regex-to-check-for-exact-word-matches-in-string\">Using Regex to Check for Exact Word Matches in String<\/h2>\n<p>While using <code>strpos()<\/code> and <code>stripos()<\/code> to look for a word is definitely faster than using regular expressions, using them when you are looking for exact word matches can be problematic. For instance, if you are looking for an exact match of the word \u201csynthesis\u201d in a sentence but it contains the word \u201cPhotosynthesis\u201d, you will get a FALSE positive with <code>strpos()<\/code> and <code>stripos()<\/code>.<\/p>\n<p>Using regular expressions can be very handy in such situations. You can use the expression <code>\\b<\/code> in a regex pattern to indicate a word boundary.  If the word you are looking for is surrounded by two <code>\\b<\/code> in a regex pattern, the <code>preg_match()<\/code> function will only match full words and return FALSE for partial matches. Here is an example:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$the_string = 'Photosynthesis and jeopardize are big words.';\n$the_word = 'synthesis';\n\n\/\/ Output \u2014 The word \"synthesis\" has an exact match in given string. [FALSE positive]\nif (preg_match('\/synthesis\/', $the_string)) {\n  echo 'The word \"synthesis\" has an exact match in given string. [FALSE positive]';\n}\n\n\/\/ Output \u2014 The word \"synthesis\" has an exact match in given string. [FALSE positive]\nif (strpos($the_string, $the_word)) {\n  echo 'The word \"synthesis\" has an exact match in given string. [FALSE positive]';\n}\n\n\/\/ Output \u2014 The word \"synthesis\" does not have an exact match in given string. [Expected Result]\nif (preg_match('\/\\bsynthesis\\b\/', $the_string)) {\n  echo 'The word \"synthesis\" has an exact match in given string. [FALSE positive]';\n} else {\n  echo 'The word \"synthesis\" does not have an exact match in given string. [Expected Result]';\n}\n<\/code><\/pre>\n<h2 id=\"using-strstr-to-check-if-string-contains-substring\">Using strstr() to Check if String Contains Substring<\/h2>\n<p>The <a href=\"..\/string\/strstr-function.php\">PHP strstr() function<\/a> can also be used to see if a word, character or substring exists within another string. This function returns the portion of the first string starting from and including the first occurrence of the word or substring that we are looking for to the end of haystack. It will return FALSE if needle is not found. This means that we can simply check if a string contains a substring by looking for the return value of <code>strstr()<\/code>. Here is an example:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$the_string = \"I am 5 years older than you.\";\n$the_word  = \"years\";\n$the_character = \"I\";\n$the_substring = \"5 years\";\n\n\/\/ Output \u2014 The word \"years\" exists in given string.\nif (strstr($the_string, $the_word) !== false) {\n  echo 'The word \"'.$the_word.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The character \"I\" exists in given string.\nif (strstr($the_string, $the_character) !== false) {\n  echo 'The character \"'.$the_character.'\" exists in given string.';\n}\n\n\/\/ Output \u2014 The substring \"5 years\" exists in given string.\nif (strstr($the_string, $the_substring) !== false) {\n  echo 'The substring \"'.$the_substring.'\" exists in given string.';\n}\n<\/code><\/pre>\n<p>Just like the <code>strpos()<\/code> function, the <code>strstr()<\/code> function also contains a case-insensitive counterpart. You can use the <code>stristr()<\/code> function to look for a word, character or substring inside another string.<\/p>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let\u2019s recap everything we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>\n<p>The best method to check if a string contains another word, character or substring is to use the <a href=\"..\/string\/strpos-function.php\">PHP strpos() function<\/a>. If you intend to perform a case insensitive search, you can use the <a href=\"..\/string\/stripos-function.php\">PHP stripos() function<\/a> instead. Both these function are easy to use and faster than other methods.<\/p>\n<\/li>\n<li>\n<p>If you are not looking for a particular substring but for a matching pattern, using regular expressions for the search might be a good idea. You can also use regular expressions for simple searches but they are slower than the <code>strpos()<\/code> function.<\/p>\n<\/li>\n<li>\n<p>Regular expression can be helpful when you are looking for an exact match of a word. For example, if you are looking for the word \u201cshy\u201d, <code>strpos()<\/code> will also return TRUE if the string contains \u201cPushy\u201d.  On the other hand, you can use the word boundary expression <code>\\b<\/code> inside the <code>preg_match()<\/code> function to only return FALSE if you are looking for \u201cshy\u201d and the string contains \u201cPushy\u201d.<\/p>\n<\/li>\n<li>\n<p>You can use other available PHP functions like <a href=\"..\/string\/strstr-function.php\">strstr()<\/a> for case-sensitive matches and <a href=\"..\/string\/stristr-function.php\">stristr()<\/a> for case-insensitive matches if you don\u2019t want to deal with TRUTHY and FALSEY values.<\/p>\n<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to check if a string contains another word, character or substring in PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s say you have a string and you want to check if it contains a specific word, character or substring. There are a lot of things that you need to be aware of in order to do the check properly without making silly mistakes. For example, using != instead of !== during the check can [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry","category-how-to-guides"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":10251,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions\/10251"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}