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.

Syntax:

strpos(string, substring, [intial_pos]);

Here,

  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

<?php
   //function to find substring in string
   //parameters
   //$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";
   }
   else{
       echo $sub_string." found at ".$index. " position";
   }
?>

Output

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”

Reference: https://www.w3schools.com/php/func_string_strpos.asp






Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




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


© https://www.includehelp.com (2015-2018), Some rights reserved.