Home » PHP

strpos() and stripos() functions in PHP

PHP strpos() and stripos() functions: In this article, we are going to learn about PHP functions strpos() and stripos() with examples.
Submitted by IncludeHelp, on January 08, 2018

These functions are used to find the position of any substring in the string. Here, strpos() is case-sensitive, while stripos() is not case sensitive.

PHP - strpos() function

This function is used to find the first occurrence of any substring in a string. This function returns an integer value which is the starting index of first occurrence of the given substring.

Note: strpos() is case sensitive.


strpos(string, substring, [intial_pos]);


  1. string – is the original string i.e. the source string in which we want to search the substring.
  2. substring – is the string which is to be searched in the string.
  3. [initial_pos] – it is an optional parameter, which may be used to define starting position, from where you want to search the substring.

Return type: the function strpos() returns an integer value which will be index of the substring in the string.

PHP code

   //function to find substring in string
   //$sub_str - string to be searched
   //$str - main string in which we have to 
   //find the substring
   function searchSubstring($sub_str, $str){
       //calling function, here third parameter is 
       //an optional - we are specifying 0 that means
       //search should start from 0th index
       $pos = strpos($str, $sub_str, 0);
       return $pos;
   //main code to test given function
   //to find substring in the string
   $main_string = "Hello world, how are you?";
   $sub_string = "how";
   //position will store in $index
   $index = searchSubString($sub_string, $main_string);
   if($index == null){
       echo $sub_string." does not exists in the string";
       echo $sub_string." found at ".$index. " position";


how found at 13 position

PHP- stripos() function

This function is same as strpos(), all operations are same except case, this function does not check case-sensitivity. stripos() ignores the case while finding any substring in the string.

Example: Refer above code and search “HOW”




Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.