Home > PHP > How to Check String Contains a Specific Word in PHP?

How to Check String Contains a Specific Word in PHP?

If you are a developer and want to check string contains a specific word or substring, then you are in right place. We will discuss here how you can check string contains a specific word in PHP. We will discuss here three methods to check it with example.

Three Methods are:

  • strpos()
  • preg_match()
  • str_contains()

With the above methods, we can check whether a substring exists in a string or not. Let’s start with these methods.

Check String with strpos() Function

The strpos() function will return the position of first occurence of substring in a string. It will return the false if the substring does not exist in the string.

Note: The strpos() function is a case-sensetive and its start the string position from ‘0‘ not from ‘1‘.

Let’s understand with an example below.

As you are seeing in the above example code, it will return true and print ‘exist‘ because the substring ‘car‘ is existing in $string. It will return the result with a case-sensitive comparison. if you want a case-insensitive comparison then you can use stripos() function in PHP.

Check String with preg_match() Function

You can also check specific word in string with preg_match(). This function used is exact match pattern in a string with regular expression search. It will return true and false as a result. If the pattern will match it will return true otherwise it will return false.

Let’s see the example.

Note: The /b specify a word boundary in the pattern.

The preg_match() function is also a case sensitive function. The above example is about a case-sensitive exact match pattern. As we searched “plat” and it will print “pattern matched” but if we will search “PLAT” or “Plat” then it will print “pattern not matched” because both words are different according to the case of the characters.

Let’s see the example with case-insensitive.

Case Insensitive Search with preg_match()

In the above examples, it will return true and print the “pattern matched“. Because it will ignore the case of the characters due to case-insensitive.

Note: The /i used for case-insensitive search. We need to put it after pattern delimeter as shown above.

Why you should use preg_match() instead strpos()?

There is no doubt that strpos() function is very fast on the performance side rather than the preg_match(). If strpos() will take 1 second to search then preg_match() will take 3 seconds, means strpos() three time faster than the preg_match().

But think, if you are checking the string contains words like “discard“, “carnet“, “vicars” and “scary” then it will return true

Let’s understand with an example.

The above example will return true and print “exist“.

Check String with str_contains() Function

The str_contains() function is a new function in PHP 8. It will also check if string contains a substring or not. It will performs as a case-sensitive and return the true false result.

It will check if the string exists in the substring then it will return true and print the “You are the best coder.” as a result.

You can also use the below functions to check string contains a specific word or not.

Hope you understand this article about check string contains a specific word in PHP. If not please comment down I will help you with that. Good Luck!

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment