Home » Programming » PHP » Php Strpos

PHP strpos Function: Check if a String Contains Another String

The PHP strpos() function checks whether a string is contained within another string – for example checking whether a certain word appears in a sentence, and what position it appears in.  It is a great companion to the PHP substr() and str_replace() functions for processing text data.

Syntax

strpos ( $haystack , $needle [, $offset = 0 ] )

Note that:

  • $haystack is the string (text or series of characters) we want to search within
  • $needle is the string search term we are looking for
  • $offset Is optional and defines where the search should start
    • If it is positive the search will start after that number of characters from the beginning of $haystack
    • If it is negative the search will start after that number of characters from the end of $haystack
  • Returns an integer representing the position of $needle within $haystack
    • The position will start at 0 – needle is at position 0 if $haystack immediately starts with $needle
    • If $needle is not found in $haystack strpos() will return FALSE
      • This is very important if using boolean logic and if statements to check whether the string was found
      • You must check that the value returned is explicitly not equal to FALSE using === to make sure the correct action is taken

Examples

As an example we can check whether a string describing a car contains keywords:

$vehicle_description = "big red shiny car";
$car_is_red = strpos($vehicle_description, "red"); // 4
$car_is_blue = strpos($vehicle_description, "blue"); // FALSE
$car_is_big = strpos($vehicle_description, "big"); // 0
$car_is_big_ignore_first_word = strpos($vehicle_description, "big", 3); // FALSE
  • $car_is_red returns 4 as the word “red” appears starting at the 4th index (position) in $vehicle_description
  • $car_is_blue returns FALSE as the word “blue” does NOT appear anywhere in $vehicle_description
  • $car_is_big returns 0 as the word “big” appears at the very beginning of $vehicle_description – which is index 0
    • In programming, 0 and FALSE are often treated as indicating a false value
    • This can’t be done when checking the results that return an index! As indexes start counting at 0 (zero), zero represents a positive result
    • If checking the result from str_pos() using an if statement, you must check explicitly whether the result is equal to FALSE to ensure the correct action is taken
      $vehicle_description = "big red shiny car"; 
      $car_is_blue = strpos($vehicle_description, "blue"); 
      if($car_is_blue === FALSE){ 
          echo "The car is not blue"; 
      }
  • To demonstrate, we use an if statement to check the presence of “blue” in $vehicle_description, and then print some text to the browser with information based on the outcome.
  • Notice we use the === operator to check both value and type so that a return of 0 does not incorrectly indicate that the string was not found

Conclusion

Knowing whether a string of text contains another string – a word or other text value – is imperative for parsing and understanding user input, or extracting useful information from database queries.

Click here for more PHP tutorials from LinuxScrew!

To view the official documentation for the PHP strpos() function:

https://www.php.net/manual/en/function.strpos.php

 

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment