What is PHP strpos() Functions and How to Use Them?

PHP strpos() and stripos() Functions
PHP strpos() Function
Basically, PHP strpos() function helps us to find the position of the first occurrence of a string in another string. This function is case-sensitive, means that it treats lower-case and upper-case characters differently.
This PHP Strpos () function returns an integer value of the position of the first occurrence of the string.

.
Syntax: strpos(original_str, search_str, start_pos)

PHP strpos(): Parameter Values

There are three parameters specified in the syntax, two-parameter are mandatory and one parameter is optional. these three parameters are described below in the given table:

Parameter Description
original_str (mandatory): Required:- Specifies the string to search, which means that this parameter refers to the original string in which we need to search the occurrence of the required string.
search_str (mandatory): Required:- Specifies the string to find, which means that this parameter refers to the string that we need to search.
start_pos (optional): Optional:- Refers to the position of the string from where the search must begin. If the start is a negative number, it counts from the end of the string.

PHP strpos(): Technical Details

Return Value: this function returns an integer value, which presents that the index of original_str (mandatory) where the string search_str (mandatory) first occurs.
Note: always String positions start from 0, and not 1.
PHP Version: 4+
.
Note:
The strpos() function is case-sensitive and this function is binary-safe.
Related functions:
strrpos() – The strrpos() function is case-sensitive, it is used to Finds the position of the last occurrence of a string inside another string.
stripos() – The stripos() function is case-sensitive, it is used to Finds the position of the first occurrence of a string inside another string.
strripos() -The strripos() function is case-sensitive, it is used to Finds Finds the position of the last occurrence of a string inside another string.

Example:-

PHP Search string: Finding a string within a string?

here to find a string within string used PHP strpos() function, it returns the index of the first occurrence of a furthermore, substring within a string (PHP search string). and its case-insensitive sibling stripos().
I think it is easier to explain in the code, let’s see the example for better understand.

as a result, it will return 8, because in the example the first character in “This is a strpos() text” which is a lowercase.

A is at index 8. I hope you know that PHP always considers the first letter of a string to be index 0,
therefore, here means that A PHP strpos() found is actually the ninth character.

you are able to specify whole words in parameter two, which will make strpos() return the first position
of that word within the PHP search string.
furthermore, let’s consider strpos($string, “text”) would return 19. here, the index of the first letter
in the matched word. on the other hand.
if the substring will be sent in parameter two then it is not found in parameter one, as a result,
PHP strpos() will return false. lets us consider the script:-

if you are trying to execute the above the program, you will find that outputs “it’s not found”,despite “This”
quite clearly being in $string (PHP search string).

therefore, here is another case sensitivity problem? Not quite. as a result, if the statement can’t tell
the difference between “substring.

It found at index 0″and “Substring it not found”, here “This” is the first thing notice that in $string,
which returns o with strpos() function.

therefore, PHP comes to the rescue with the === operator, which means that $pos must be equal to false and of
the same type as false (boolean).

If the PHP strpos()will return 0, this will return 0 when “This” is found in $string but it will be of
type integer. if statement to use === rather than ==, it checks the value of 0 and false find,
furthermore, they find that they do not match the latter is a boolean and the former is an integer and they
match (both false). Therefore let us see the corrected version of the script given below:-

after that see the next script, now consider, which tries to match the “i” in “is”:

therefore, in PHP search string, here the problem is that PHP strpos() match with the first “i”
it comes across, which will be in “this”.
it’s is the 3rd parameter to strpos() which allows specifying where to start from. as the “i” in “This” is at index 2, therefore here we need to specify one place after that (3).
furthermore, it will report back the next “i” after it and the start position for strpos().for example see the given below script:-

as a result in PHP search string, it will print “found at 5!”, and which is the position of the “i” in “is”.