Hit enter after type your search item

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:


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.

PHP strpos

See online demo and code

The PHP code:


 

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:

PHP strpos search

See online demo and code

For 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 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.:


 

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:


 

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:


 

 

 

Also see: search string in PHP with ajax

This div height required for enabling the sticky sidebar