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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax of PHP strpos
Since this can be used in various ways, we can see the various syntax:
strpos($string, $toBeSearched)
Parameter Description:
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.
Given below are the examples mentioned :
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.
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.
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.
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:
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.
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.
The above is the detailed content of PHP strpos. For more information, please follow other related articles on the PHP Chinese website!