Updated April 20, 2023
Introduction to PHP strpos
The following article provides an outline for PHP strpos. There is a large set of built-in functions to make string related operations easier for the coder or the developer. The strops() is one of the popular string functions we can use as per requirements as its name suggests for dealing with the position in any string in PHP. Using this function, we can get the position of the string inside another string. This function can be used in various ways with various scenarios like we can get the first occurrence position of a string inside another. We can get the last occurrence position of the string inside another string. This function also can be used to search one string into another string.
Syntax of PHP strpos
Since this can be used in various ways, we can see the various syntax:
strpos($string, $toBeSearched)
Parameter Description:
- $string: This is a string upon which the search needs to be done.
- $toBeSearched: This is a string that needs to search inside the above string.
Return Type:
It returns the numeric value if the search finds it; otherwise it gives the false.
stripos($string, $toBeSearched, $startPostion)
We can see one additional parameter apart from the earlier one. $startPostion is nothing, but it’s all about where the search will be started.
strrpos($string, $toBeSearched, $startPostion)
This function can be used to search string into another string. This start was searching in the reverse order. The word ‘r’ in the middle of this function does the trick of searching the string into another string in the reverse order.
How does strpos() Function work in PHP?
- To use the strpos() function and other related functions to it, we need to have two strings, one string will be searched into another string.
- We have various other built-in functions like stripos(), strrpos(), strripos() etc. stripos() can be used to search the string of a case-insensitive search string. Strrpos() can be used to search the string in reverse order.
- The strripos() can be used to search the sting in the reverse order of a case-insensitive search string.
Examples of PHP strpos()
Given below are the examples mentioned :
Example #1
A basic program using strops() function with only two parameters, string one and the search string.
Code:
<?php
$string = "I love php, I love coding in php too!";
$searchString = "php";
$output = strpos($string, $searchString);
echo "String:. “ $string;
echo "<br>Output: ". $output;
?>
Output:
In the above example, we can see that in a string we are trying to search a PHP string, the use of strops() for searching the PHP word gives the output 7. 7 is the index of the first occurrence of the PHP word. The strops() is a case-sensitive function to find any string inside another string.
Example #2
Let’s try to replicate the same as in example 1 but with the PHP’s different case (upper or lower).
Code:
<?php
$string = "I love php, I love coding in php too!";
$searchString = "PHP";
$output = strpos($string, $searchString); // this will give the false result
echo "String: ". $string;
echo "<br>Output: ". $output;
$output1 = stripos($string, $searchString); // this will give the the 7 as a result
echo "<br>String: ". $string;
echo "<br>Output: ". $output1;
?>
Output:
In the above example, we can see that if the case if not matching in this case the strops() will not be doing the job, for this, we have stripos() with the same syntax and the parameter for doing the same job.
Example #3
Let’s see an example of using the third parameter as well with the strops().
Code:
<?php
$string = "I love php, I love coding in php too!";
$searchString = "PHP";
$output = strpos($string, $searchString,8); // this will give the false result
echo "String: ". $string;
echo "<br>Output: ". $output;
$output1 = stripos($string, $searchString,8); // this will give the the 7 as a result
echo "<br>String: ". $string;
echo "<br>Output: ". $output1;
?>
Output:
Just a little different from the previous example gives a huge difference as we can see in the result. We have put the 8 as a third parameter. Using 8 as a third parameter means the search will begin from position 8, not from position 0.
Example #4
Let’s see a bit of a professional example (handling types of return).
Code:
<?php
$string = "https://www.educba.com/tutorials/?source=menu";
$searchString = "tutorials";
$output = strpos($string, $searchString); // this will give the false result
if($output==false){
echo "Search not found.";
}else{
echo "Search found.";
}
?>
Output:
Example #5
strrpos() can be used to search the position of the string into another string in the reverse order.
Code:
<?php
$string = "I love PHP; I like to code in PHP";
$searchString = "PHP";
$output = strrpos($string, $searchString); // this will give the false result
if($output==false){
echo "Search not found.";
}else{
echo "Search found: ".$output;
}
?>
Output:
The same way, we can search the string into another of case-insensitive search string using the strripos() function. Now, we have seen the example of all the types of strops() we can use per the business requirements.
Conclusion
In PHP programming language, we can use the strpose() and the stripos() built-in functions to search a string into another. The strops() is a case-sensitive function to deal with the searching, and in PHP stripos() can be used for case-insensitive to do the same job like strops(). The stripos() don’t check for the same case. The developer must be careful enough while using these functions because if the searches match, it gives the numeric as a result but in case no search is found, it returns false.
Recommended Articles
This is a guide to PHP strpos. Here we have discussed the introduction to PHP strpos, and how strpos() function works with examples. You may also have a look at the following articles to learn more –