Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»PHP»How to check if string contains substring in PHP

    How to check if string contains substring in PHP

    By RahulApril 9, 20233 Mins Read

    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:

    1
    strpos(string $haystack, string $needle, int $offset = 0): int|false

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

    1
    2
    3
    4
    5
    6
    7
    8
    $haystack = "Hello, welcome to the PHP world!";
    $needle = "PHP";
     
    if (strpos($haystack, $needle) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found!";
    }

    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:

    1
    strstr(string $haystack, string $needle, bool $before_needle = false): string|false

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

    1
    2
    3
    4
    5
    6
    7
    8
    $haystack = "Hello, welcome to the PHP world!";
    $needle = "PHP";
     
    if (strstr($haystack, $needle) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found!";
    }

    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:

    1
    stripos(string $haystack, string $needle, int $offset = 0): int|false

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

    1
    2
    3
    4
    5
    6
    7
    8
    $haystack = "Hello, welcome to the PHP world!";
    $needle = "php";
     
    if (stripos($haystack, $needle) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found!";
    }

    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:

    1
    stristr(string $haystack, string $needle, bool $before_needle = false): string|false

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

    1
    2
    3
    4
    5
    6
    7
    8
    $haystack = "Hello, welcome to the PHP world!";
    $needle = "php";
     
    if (stristr($haystack, $needle) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found!";
    }

    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:

    1
    preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false

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

    1
    2
    3
    4
    5
    6
    7
    8
    $haystack = "Hello, welcome to the PHP world!";
    $needle = "/php/i"; // Case-insensitive search
     
    if (preg_match($needle, $haystack)) {
        echo "Substring found!";
    } else {
        echo "Substring not found!";
    }

    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:

    1
    mb_strpos(string $haystack, string $needle, int $offset = 0, string $encoding = null): int|false

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $haystack = "Hello, welcome to the PHP world!";
    $needle = "PHP";
    $encoding = "UTF-8";
     
    if (mb_strpos($haystack, $needle, 0, $encoding) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found!";
    }

    7. Using mb_stripos()

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

    Syntax:

    1
    mb_stripos(string $haystack, string $needle, int $offset = 0, string $encoding = null): int|false

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $haystack = "Hello, welcome to the PHP world!";
    $needle = "php";
    $encoding = "UTF-8";
     
    if (mb_stripos($haystack, $needle, 0, $encoding) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found!";
    }

    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.

    PHP string substring
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Schedule a Cron Job for PHP: A Step-by-Step Guide

    How to Compare Two Array Values in PHP: A Practical Guide

    How to Remove Substrings Using ${} in Bash Scripts

    View 5 Comments

    5 Comments

    1. Mern on February 24, 2020 4:09 pm

      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?

      Reply
    2. jen on July 2, 2019 7:23 am

      Just a test comment, but a nice article.

      Reply
    3. Vikas Tyagi on January 1, 2019 6:29 am

      thanks for nice article

      Reply
    4. Kim Kline on June 28, 2018 6:01 pm

      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

      Reply
      • Rahul K. on June 29, 2018 1:51 am

        Thank you Kim, I have updated tutorial.

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Setting and Getting the Default Timezone in Python
    • What is Media Access Control (MAC) Address?
    • What is Cross-Site Scripting (XSS)?
    • What is Content Security Policy (CSP)?
    • A User’s Guide to Understanding Redirection Operators in Bash
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.