One of the most common tasks in PHP programming is to determine whether a string contains a particular substring. This operation is essential for various applications, such as searching for keywords in user input, validating user data, or filtering strings based on certain conditions.

Advertisement

In this article, we will explore different techniques to check if a string contains a substring in PHP. We will cover functions like strpos(), strstr(), stripos(), and stristr() and explain their use-cases, advantages, and limitations.

1. Using strpos()

The strpos() function is a popular and efficient method for checking if a string contains a substring. It returns the position of the first occurrence of the substring in the main string or FALSE if the substring is not found.

Syntax:

Here’s an example of how to use strpos():

2. Using strstr()

The strstr() function returns the part of the main string starting from the first occurrence of the substring, or FALSE if the substring is not found. To check if a string contains a substring, you can simply check if the result is not FALSE.

Syntax:

Here’s an example of how to use strstr():

3. Using stripos()

The stripos() function is similar to strpos(), but it is case-insensitive. It returns the position of the first occurrence of the substring in the main string, regardless of the case, or FALSE if the substring is not found.

Syntax:

Here’s an example of how to use stripos():

4. Using stristr()

The stristr() function is a case-insensitive version of strstr(). It returns the part of the main string starting from the first occurrence of the substring, regardless of the case, or FALSE if the substring is not found.

Syntax:

Here’s an example of how to use stristr():

5. Using preg_match()

If you need more flexibility and want to search for substrings using regular expressions, you can use the preg_match() function. It searches for a pattern in a given string and returns 1 if the pattern is found or 0 if it’s not found.

Syntax:

Here’s an example of how to use preg_match():

6. Using mb_strpos()

For multibyte string support, you can use the mb_strpos() function, which works similarly to strpos() but supports a wider range of character encodings.

Syntax:

Here’s an example of how to use mb_strpos():

7. Using mb_stripos()

The mb_stripos() function is a case-insensitive version of mb_strpos() and supports multibyte character encodings.

Syntax:

Here’s an example of how to use mb_stripos():

Conclusion

In this article, we have covered seven different methods – strpos(), strstr(), stripos(), stristr(), preg_match(), mb_strpos(), and mb_stripos() – for checking if a string contains a substring in PHP. Depending on your application’s specific needs and character encoding requirements, you can choose the most suitable method. Remember to use strict comparisons (i.e., !== false) when checking the results of these functions, and consider using preg_match() if you need to search for substrings using regular expressions.

Share.

5 Comments

  1. Hi, i have a rather twisted task similar to this post. If i want to check for sub strings in a string and want to put spaces and whole words to account, how do i do that? For example; the string “This is main string”, if i use it to check for the sub string “Th” it should not be true unless i put the whole word “This”, and if i want to get the phrase “is main” it should return true. How can i achieve that in php?

  2. Example 3 should use === instead of ==.

    If $str does NOT contain the substring ‘This‘, then strpos($str, ‘This’) will return false.
    Therfore, strpos($str, ‘This’) == 0 evaluates to true

Leave A Reply


Exit mobile version