PHP strpos() function


The strpos() function in PHP is used to find the position of the first occurrence of a substring within a string. If the substring is found, strpos() returns the position (index) of the first occurrence of the substring. If the substring is not found, it returns false.

Syntax:

strpos(string $haystack, string $needle, int $offset = 0): int|false
  • $haystack: The string to search in.
  • $needle: The substring to search for.
  • $offset (optional): The position in the string where the search should start. Default is 0 (the beginning of the string).

Return Value:

  • If the substring is found, strpos() returns the numeric position (starting from 0).
  • If the substring is not found, it returns false.

Important Note:

  • Since strpos() can return 0 (if the substring is found at the start of the string), you should use the === operator to check the return value to avoid confusion between 0 (found at the start) and false (not found).

Example 1: Basic Usage

<?php $haystack = "Hello World"; $needle = "World"; $position = strpos($haystack, $needle); if ($position !== false) { echo "The string 'World' was found at position: " . $position; } else { echo "The string 'World' was not found."; } ?>

Output:

The string 'World' was found at position: 6

Explanation: The substring "World" starts at index 6 in the string "Hello World".

Example 2: Substring Not Found

<?php $haystack = "Hello World"; $needle = "PHP"; $position = strpos($haystack, $needle); if ($position !== false) { echo "The string 'PHP' was found at position: " . $position; } else { echo "The string 'PHP' was not found."; } ?>

Output:

The string 'PHP' was not found.

Explanation: The substring "PHP" does not exist in the string "Hello World", so strpos() returns false.

Example 3: Starting the Search at a Specific Position (Offset)

<?php $haystack = "Hello World, welcome to the World!"; $needle = "World"; $position = strpos($haystack, $needle, 10); // Start search after index 10 if ($position !== false) { echo "The string 'World' was found at position: " . $position; } else { echo "The string 'World' was not found."; } ?>

Output:

The string 'World' was found at position: 24

Explanation: By specifying an offset of 10, strpos() starts the search after the first occurrence of "World". The next occurrence of "World" starts at position 24.

Example 4: Using Strict Comparison (===) to Avoid False Positives

<?php $haystack = "Hello World"; $needle = "Hello"; $position = strpos($haystack, $needle); if ($position === 0) { echo "The string 'Hello' was found at the beginning!"; } else { echo "The string 'Hello' was not found at the beginning."; } ?>

Output:

The string 'Hello' was found at the beginning!

Explanation: Since "Hello" is at the very beginning of "Hello World", the strpos() function returns 0. To avoid confusion with false, we use the strict comparison (===) to correctly detect the position as 0.

Example 5: Case Sensitivity

The strpos() function is case-sensitive, meaning it treats uppercase and lowercase letters as different characters.

<?php $haystack = "Hello World"; $needle = "world"; // Lowercase 'world' $position = strpos($haystack, $needle); if ($position !== false) { echo "The string 'world' was found at position: " . $position; } else { echo "The string 'world' was not found."; } ?>

Output:

The string 'world' was not found.

Explanation: The lowercase "world" is not found because strpos() is case-sensitive. To make a case-insensitive search, you can use stripos() instead.


Key Points:

  • strpos() returns the position of the first occurrence of a substring, or false if not found.
  • Use the strict comparison operator (===) to distinguish between false (not found) and 0 (found at the start of the string).
  • The search can be started from a specific position using the optional offset parameter.
  • It is case-sensitive by default.

This function is commonly used for tasks like searching, filtering, and string manipulation in PHP.