Search strings by PHP strpos / stripos functions | 4 demos
The strpos function of PHP
The 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:
1 |
$position = strpos($given_string, $search_string); |
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 function
See 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $source_string = 'This is a strpos Tuotrial which is used to search strings. It tells whether a given string contains a search string or not!'; $search_term = 'strpos'; $posistion = strpos($source_string, $search_term); if ($posistion === false) { echo "The source string does not contain the: '$search_term'!"; } else { echo "The string contains the search term: '$search_term'! </BR>"; echo " The given substr found at: $posistion"; } ?> |
The output of the above code will be:
The string contains the search term: ‘strpos’!
The given substring found at: 10
An example of PHP strpos with user entered search term
This 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 code
For the demo, I have used following source string:
1 |
$source_string = 'In this demo, I am using a user entered search term to check if string contains search term or not by using strpos!'; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php $search_term=$_POST["searchterm"]; $source_string = 'In this demo, I am using a user entered search term to check if string contains search term or not by using strpos!'; // = 'strpos'; $posistion = strpos($source_string, $search_term); if( $search_term ){ echo "<div class='result'>"; if ($posistion === false) { echo "The source string does not contain the: '$search_term'!"; } else { echo "The string contains the search term: '$search_term'! </BR>"; echo " The given substring found at: $posistion"; } echo "</div>"; } ?> |
If you are interested about the markup for presentation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<!doctype html> <html> <head> <style> .result { background: #5BB75B; height: auto; width:220px; border-radius: 15px; padding:20px; font-size:20px; color:#000; } .searchterm{ background: #6A0432; width:220px; border-radius: 15px; padding:20px; font-size:18px; color:#fff; } </style> <div class="searchterm"> <form method="post" action=""> <label>Enter a string:</label><input type="text" name="searchterm"> <input type="submit"/> </form> </div> </body> </html> |
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 function
As 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.:
1 |
$position = stripos($given_string, $search_string); |
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 values
A source string is created with the following string in this demo:
1 |
$source_string = 'This is a stripos Tutorial which is used to search strings. It tells whether a given string contains a search string or not!'; |
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 code
Example with user entered search term
This 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 code
Only this line of code is changed than above example of strpos:
1 |
$posistion = stripos($source_string, $search_term); |
Also see: search string in PHP with ajax