PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Query SubString in A String

To find a substring within a string in PHP, you can use several built-in functions. Here are a few of the most commonly used ones:

  • strpos(): This function finds the position of the first occurrence of a substring in a string. It returns false if the substring is not found.
$str = "Hello, world!";
$pos = strpos($str, "world");

if ($pos !== false) {
    echo "The string 'world' was found at position $pos";
} else {
    echo "The string 'world' was not found";
}

Note that strpos() is case-sensitive. If you want a case-insensitive search, you can use stripos() instead.

  • strstr(): This function finds the first occurrence of a substring in a string and returns all content from the substring to the end of the string. It returns false if the substring is not found.
$str = "Hello, world!";
$sub = strstr($str, "world");

if ($sub !== false) {
    echo "The substring 'world' was found and the rest of the string is: $sub";
} else {
    echo "The string 'world' was not found";
}

Again, strstr() is case-sensitive. If you want a case-insensitive search, you can use stristr() instead.

  • substr_count(): This function counts the number of occurrences of a substring in a string.
$str = "Hello, world! The world is round.";
$count = substr_count($str, "world");

echo "The string 'world' was found $count times";

Remember that string positions in PHP start from 0, not 1. So if strpos() returns 0, it means the substring was found at the beginning of the string. That's why when checking the return value of strpos() or strstr(), you should use !== false rather than != false, because 0 is considered false when using !=, but not when using !==.

  1. Query substring presence in PHP:

    • To determine if a string contains a specific substring, various functions like strpos() and strstr() can be used.
    // File: substring_presence_query.php
    $haystack = "Searching for substring in PHP";
    $needle = "substring";
    
    if (strpos($haystack, $needle) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found.";
    }
    // Output: Substring found!
    
  2. Check if a string contains a substring in PHP:

    • The strpos() function can be employed to check if a string contains a particular substring.
    // File: check_substring_presence_strpos.php
    $haystack = "Check for substring in PHP";
    $needle = "substring";
    
    if (strpos($haystack, $needle) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found.";
    }
    // Output: Substring found!
    
  3. PHP strpos() function for substring search:

    • The strpos() function returns the position of the first occurrence of a substring in a string.
    // File: strpos_substring_search.php
    $haystack = "PHP strpos function example";
    $needle = "strpos";
    
    $position = strpos($haystack, $needle);
    
    if ($position !== false) {
        echo "Substring found at position: $position";
    } else {
        echo "Substring not found.";
    }
    // Output: Substring found at position: 4
    
  4. Case-insensitive substring search in PHP:

    • For a case-insensitive search, use functions like stripos().
    // File: case_insensitive_strpos.php
    $haystack = "Case-Insensitive Substring Search";
    $needle = "search";
    
    if (stripos($haystack, $needle) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found.";
    }
    // Output: Substring found!
    
  5. Determine position of substring in PHP:

    • Use strpos() to find the position of the first occurrence of a substring.
    // File: determine_position_strpos.php
    $haystack = "Position of Substring in PHP";
    $needle = "Substring";
    
    $position = strpos($haystack, $needle);
    
    if ($position !== false) {
        echo "Substring found at position: $position";
    } else {
        echo "Substring not found.";
    }
    // Output: Substring found at position: 12
    
  6. PHP strstr() for substring query:

    • The strstr() function returns the part of the string from the first occurrence of the substring.
    // File: strstr_substring_query.php
    $haystack = "Using strstr() in PHP";
    $needle = "strstr";
    
    $result = strstr($haystack, $needle);
    
    if ($result !== false) {
        echo "Substring found: $result";
    } else {
        echo "Substring not found.";
    }
    // Output: Substring found: strstr() in PHP
    
  7. Substring existence check in PHP:

    • Check if a string contains a substring using functions like strpos().
    // File: substring_existence_check.php
    $haystack = "Existence of Substring in PHP";
    $needle = "Substring";
    
    if (strpos($haystack, $needle) !== false) {
        echo "Substring found!";
    } else {
        echo "Substring not found.";
    }
    // Output: Substring found!
    
  8. Handling multiple occurrences of a substring in PHP:

    • To handle multiple occurrences, use a loop with functions like strpos().
    // File: multiple_occurrences_handling.php
    $haystack = "PHP has multiple substring occurrences in PHP";
    $needle = "PHP";
    $position = 0;
    
    while (($position = strpos($haystack, $needle, $position)) !== false) {
        echo "Substring found at position: $position\n";
        $position += strlen($needle);
    }
    // Output:
    // Substring found at position: 0
    // Substring found at position: 25
    // Substring found at position: 46