What is the use of strpos () function in php?
Show
The strpos function of PHPThe PHP strpos is a string function which is used to search a substring in a given string. It returns the numeric value of the first occurrence of specified search string. Syntax to use strpos This is how you can use the strpos function in PHP:
Note: The strpos function searches the string as case sensitive. So “Test” and “test” search term will have different meanings. Also, the position starts at 0 and not 1. I will show you hard coded and user entered search terms demos in this tutorial to use the strpos function to demonstrate how it works. A simple example to use strpos functionSee the following example where I have used hard-coded strings to demonstrate the strpos PHP string function. See online demo and code The PHP code:
The output of the above code will be:
An example of PHP strpos with user entered search termThis method can be quite useful for certain scenarios like your web form does not allow certain words and you want to check before saving information in the database. Also, you may check whether a search term given by the user is contained in source string that might be a database driven string. On that basis, you can show certain results as a response. In this demo, a user can enter a term in the textbox field. As the button is pressed after entering the substring, the strpos function will be used to check whether source string contains the substring? It will display the message accordingly. See the demo online: See online demo and codeFor the demo, I have used following source string:
So try different letters or words as search string that the above string contains or try with other words to return false. Also, try terms with capital or small letters to check the difference. Following PHP code was used:
If you are interested about the markup for presentation:
You can see the complete code in the demo page source code area. Similarly, you can use a database driven string as a source to build search feature in your website. I have also written a guide for using strpos PHP string function with jQuery ajax method. (see the link at bottom) Case insensitive search by using stripos functionAs mentioned earlier, the strpos is a case sensitive function to search strings in PHP. To search string case insensitively, use the stripos PHP function. The syntax is almost the same as strpos, e.g.:
Let me show you how PHP stripos method works with the almost similar examples as I used in above examples. This time replacing the strpos by stripos function. An example of stripos with hard-coded valuesA source string is created with the following string in this demo:
While the search term is used: $search_term = ‘tutorial’; Although, capital letter is used in the source string, see the output online: See online demo and codeExample with user entered search termThis example is also the same as used for strpos method, however, the stripos method is used in it. Try existing letters/words with difference cases and see the output. See online demo and codeOnly this line of code is changed than above example of strpos:
Also see: search string in PHP with ajax How does Strpos work in PHP?The strpos() function finds the position of the first occurrence of a string inside another string. Note: The strpos() function is case-sensitive. Note: This function is binary-safe.
What is the use of strlen () and strpos () functions in PHP?PHP String Functions. What is the use of EM Strpos )The strpos() function finds the position of the first occurrence of a string inside another string. The stripos() function finds the position of the first occurrence of a string inside another string. 2. It is case-sensitive function.
What is the use of strlen () and strops () functions?The strlen() function returns the length of a function. The strcmp() function compares two strings.
|