PHP strpos / stripos Functions

An image showcasing PHP strpos functionality

The strpos function of PHP

The PHP strpos is a string function that is used to search a substring in a given string.

It returns the numeric value of the first occurrence of the specified search string.

Syntax to use strpos

This is how you can use the strpos function in PHP:

$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.

The PHP code:

<?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 a source string that might be database-driven.
  • 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.

PHP strpos search

Online demo and code

For the demo, I have used the following source string:

$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.

The following PHP code was used:

<?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 in the markup for the presentation:

<!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 on your website. I have also written a guide for using strpos PHP string function with the jQuery ajax method. (see the link at the 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.:

$position = stripos($given_string, $search_string);

Let me show you how the PHP stripos method works with almost similar examples as I used in the above examples. This time replacing the strpos with stripos function.

An example of stripos with hard-coded values

A source string is created with the following string in this demo:

$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, the capital letter is used in the source string – see the code/output:

PHP Code:

$source_string = 'This is a stripos Tuotrial which is used to search strings. It tells whether a given string contains a search string or not!';
$search_term   = 'tuotrial';
$posistion = stripos($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";
}

Output:

The string contains the search term: ‘tuotrial’!
The given substr found at: 18

An 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 different cases and see the output.

Online demo and code

Only this line of code is changed than the above example of strpos:

$posistion = stripos($source_string, $search_term);

PHP code of the above example:

$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 stripos!';
$posistion = stripos($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>";
}

The markup:

<!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>
</head>
<div class="searchterm">

<form method="post" action="">
<label>Enter a string:</label><input type="text" name="searchterm">
<input type="submit"/>
</form>
</div>
</body>
</html>

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!