What is the use of strpos () function in php?

  • 1. The strpos function of PHP
  • 2. A simple example to use strpos function
  • 3. An example of PHP strpos with user entered search term
  • 4. Case insensitive search by using stripos function
  • 5. An example of stripos with hard-coded values
  • 6. Example with user entered search term

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:

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

What is the use of strpos () function in php?

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

$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'!
"
;

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:

What is the use of strpos () function in php?

See online demo and code

For the demo, I have used 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.

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

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

";

if($posistion===false) {

echo"The source string does not contain the: '$search_term'!";

}else{

echo"The string contains the search term: '$search_term'!
"
;

echo" The given substring found at: $posistion";

}

echo "

";

}

?>

 

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

class="searchterm">

method="post" action="">

Enter a string:type="text"name="searchterm">

type="submit"/>

 

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

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

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

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

 

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.