How to Check if a PHP String Contains Another?

In most object-oriented languages, you will need to check if a string contains another string, a substring, or a character.This is a common question asking you to check whether a PHP string contains another string in a programming interview. Many armature programmers may think that checking for the existence of a string in another string is very challenging, but truth be told, checking if a string contains substring is one of the easiest tasks in programming. This article will comprehensively describe how to check if a PHP string contains a substring or another string.

How to Check if a PHP String Contains another String

There are several ways to verify if a PHP string contains another string. You can use these string functions to know if a PHP string contains substring or a character:

  1. strstr – this function is used to return the first occurrence of a string in a PHP string. The strstr( ) function uses the following syntax to enable PHP to find a string in another string:
    char * strstr ( const char *, const char * );
    char * strstr ( char * str1, const char * str2 );
  2. stristr – the stristr function is similar to the strstr one, only that it is case-insensitive. It locates a pointer to the first occurrence without considering case sensitivity.
  3. strrchr – this C library function searches for the last occurrence of c (the character) in a string. The syntax for this function is char *strrchr(const char *str, int character);
  4. strpos – the strpos() function returns the position of the first occurrence of a string in another string. This function is commonly used as it is the easiest method to assist PHP find string in string.
  5. strpbrk – this is another inbuilt string function that returns a string from a character identical to any character specified in a PHP string. This function uses the syntax char *strpbrk(const char *s1, const char *s2).

Example

In our example, we will use the strpos() function technique to find whether a PHP string contains another string. This approach is mostly preferred because it is faster and demands less memory.

Assumptions: Let’s assume we have one string called $flashlight and another string called $flash. This is how the strpos function will work:

$flashlight = ‘How are you?’;

if (strpos($flashlight, ‘are’) !== false) {
echo ‘true’;
}

The following code is used when dealing with the strstr() function technique:

int main()
{
const char flashlight [20] = “TutorialsPoint”;
const char flash[10] = “Point”;
char *ret;

ret = strstr(flashlight, flash);

printf(“The substring is: %s\n”, ret);

return(0);
}

Keep in mind that the strstr() function is case-sensitive so you would want to use the stristr() function when performing a case-insensitive search. However, you can wrap your strings with the strtolower() function as it converts the strings into lower case which assists in eliminating problems that come with a case-sensitive string.

Recommended read: since you’re here, you might also be interested in our collection of 14 PHP interview questions!

Conclusion

In a PHP if string contains string, you can find that out by using the strpos() function. Alternatively, you can use the strstr() function if the string contains a case-sensitive character and the stristr() function for a case-insensitive search. We hope that this article helps you check for a string or substring in a PHP string. Feel free to give any suggestions or ask questions about this article in our discussion forum.

Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.