stripos Explained: Case-Insensitive Search for Substrings


Functionality

  • If the needle is not found within the haystack, stripos returns FALSE.
  • If the needle is found, stripos returns the character position (index) within the haystack string where the first occurrence of the needle begins.
  • The search is case-insensitive, meaning uppercase and lowercase letters are treated the same.
  • It searches the haystack string for the first appearance of the entire needle substring.

Optional Offset

  • stripos also accepts an optional third argument, offset. This specifies the position in the haystack string where the search should start. By default, the search starts from the beginning (offset of 0).

Example

$haystack = "Hello, WORLD!";
$needle = "world";

$pos = stripos($haystack, $needle);

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

This code would output:

The word 'world' was found at position 7
  • If you need to explicitly use stripos for compatibility reasons, ensure you're working with single-byte character encodings.
  • For PHP versions since 7.3.0, stripos is not recommended due to potential unexpected behavior. It's suggested to use mb_stripos instead, which provides a more robust case-insensitive search that handles multibyte characters correctly.


Using offset

$haystack = "This is a TEST String";
$needle = "test";

$pos_default = stripos($haystack, $needle);
$pos_offset = stripos($haystack, $needle, 10); // Start search from index 10

echo "Default search: Position at $pos_default\n";
echo "Offset search: Position at ";

if ($pos_offset !== false) {
  echo $pos_offset;
} else {
  echo "FALSE (not found after index 10)";
}

This code searches for "test" in the string. The first search uses the default offset (0) and the second starts searching from index 10.

Handling not found case

$haystack = "This string does not have apple";
$needle = "apple";

$pos = stripos($haystack, $needle);

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

This code checks if "apple" exists in the string and displays an appropriate message based on the result.

$haystack = "Hello World";
$needle = "";

$pos = stripos($haystack, $needle);

if ($pos !== false) {
  echo "An empty string was found at position $pos"; // Since empty string is always at the beginning
} else {
  echo "An empty string is not considered for searching";
}


mb_stripos (Recommended)

  • The syntax is similar to stripos:
  • It offers more robust functionality compared to stripos by handling multibyte characters correctly.
  • This function is the preferred way to perform a case-insensitive search in PHP, especially for versions 7.3.0 and later.
$pos = mb_stripos($haystack, $needle);

strcasecmp + strpos

  • It's less efficient than mb_stripos but can be used for compatibility with older code.
  • This approach combines two functions:
    • strcasecmp: Performs a case-insensitive string comparison.
    • strpos: Finds the position of a substring within a string.
$pos = strpos(strtolower($haystack), strtolower($needle));

if ($pos !== false && strcasecmp($haystack, $needle) === 0) {
  // Needle found with correct case
}

Regular Expressions (Complex scenarios)

  • This approach requires more knowledge of regular expressions but offers greater flexibility.
  • For complex case-insensitive searches with specific patterns, you can use regular expressions with the i modifier (case-insensitive).
  • For complex case-insensitive pattern matching, regular expressions might be suitable, but ensure you understand their syntax and potential performance implications.
  • If compatibility with older code is a concern, consider strcasecmp combined with strpos.
  • If you're working with a modern PHP version and potentially dealing with multibyte characters, use mb_stripos.