PHP strpos() Function

What is PHP strpos() Function?

If you want to know the position of a substring in a string, use strpos() function. It lets you know the position of the first occurrence of the substring, if the string contains it multiple times. If the substring consists of multiple characters rather than a single character, the function returns the position number of the first character of the matching substring.

The name of the string “strpos” came due to its dealing with the position in a string.

Syntax:

strpos(string, substring, start_position)

Parameters:

The Function has 2 required parameters and 1 optional parameter-

string (Required): It is the string in which the function finds the substring.

substring (Required): It is the substring that the function finds in the string. Check example 1.

start_position (Optional): It refers to the number of character from where the function begins the search. If you don’t want the function start searching from the beginning rather from a specific position, then use this parameter. The function considers 0 as the position of the first character. It could be a positive, a 0 or a negative number-

  • If it is 0, the function begins searching from the first character of the string. Check example 2.
  • If it is a positive number, the function begins searching from this number of character counted from the beginning of the string and ends at the right of the string. Check example 3.
  • If it is a negative number, the function begins searching from this number of character counted from the end of the string and ends at the right of the string. This is like finding the substring at the specified end of the string. Check example 4.

Return Values:

The function returns the position number of the character where it finds the substring counted from the beginning of the string. Remember, the function considers the position of the first character as 0.

If the function doesn’t find the substring in the string, it returns false.

Examples:

Example 1: Finding substring’s position in the string with strpos() function-

<?php
$string = "'CSS for Beginners' is the first course in CSS.";
$substring = "CSS";
$position = strpos($string, $substring);
echo ($position >=0) ? "'$substring' exists at position $position in the '$string'." :  "'$substring' doesn't exist in '$string'.";
?>

Output:

‘CSS’ exists at position 1 in the ”CSS for Beginners’ is the first course in CSS.’.

Explanation:

The function finds the substring “CSS” at the 0th position in the string.

Example 2: Start searching for the substring at position 0 in the string using strpos()-

<?php
$string = "'JavaScript for Beginners' is the next course after CSS";
$substring = "JavaScript";
$position = strpos($string, $substring, 0);
if($position === FALSE){echo "'$substring' doesn't exist in '$string'.";}
else{echo "'$substring' exists at position $position in the '$string'.";}
?>

Output:

‘JavaScript’ exists at position 1 in the ”JavaScript for Beginners’ is the next course after CSS’.

Explanation:

The function starts finding the substring “JavaScript” at the 0th position in the string and finds at that position.

Example 3: Finding the substring from a specific character in the string with strpos() function-

<?php
$string = "'CSS for Beginners' is the first course in CSS.";
$substring = "CSS";
$position = strpos($string, $substring, 10);
if($position === FALSE){echo "'$substring' doesn't exist in '$string'.";}
else{echo "'$substring' exists at position $position in the '$string'.";}
?>

Output:

‘CSS’ exists at position 43 in the ”CSS for Beginners’ is the first course in CSS.’.

Explanation:

The function starts finding the substring “CSS” from the 10th character which is the “e” in the string and find it at position 43. Note, as it starts finding from 10th character, it doesn’t consider the first “CSS” that exists in the beginning as the first occurrence of this substring.

Example 4: Finding the substring in a specific last portion in the string with strpos() function-

<?php
$string = "Start learning with 'HTML for Beginners'?";
$substring = "HTML";
$position = strpos($string, $substring, -25);
if($position === FALSE){echo "'$substring' doesn't exist in '$string'.";}
else{echo "'$substring' exists at position $position in the '$string'.";}
?>

Output:

‘HTML’ exists at position 21 in the ‘Start learning with ‘HTML for Beginners’?’.

Explanation:

The function starts finding the substring “HTML” in the last 25 characters.

Example 5: Finding a string of opposite case using strpos() function-

<?php
$string = "Learn PHP in-depth";
$substring = "php";
$position =  strpos($string, $substring);
if($position === FALSE){echo "'$substring' doesn't exist in '$string'.";}
else{echo "'$substring' exists at position $position in the '$string'.";}
?>

Output:

‘php’ doesn’t exist in ‘Learn PHP in-depth’.

Explanation:

The function can’t consider “php” and “PHP” same, so it can’t find the substring “php” in the string.

Example 6: Handling binary data with strpos() function-

<?php
$string = "Did you check '\x50\x48\x50 for Beginners'?";
$substring = "PHP";
$position = strpos($string, $substring);
if($position === FALSE){echo "'$substring' doesn't exist in '$string'.";}
else{echo "'$substring' exists at position $position in the '$string'.";}
?>

Output:

‘PHP’ exists at position 15 in the ‘Did you check ‘PHP for Beginners’?’.

Explanation:

Hexadecimal ‘\x50’ and’\x48’ are same as “P” and “H” characters. The function strpos() can understand this and can find the substring “PHP” (\x50\x48\x50) in the string.

Example 7: Avoiding FALSE positive result with strpos() function-

<?php
$string = "Learn PHP in-depth";
$substring = "Learn";
$position =  strpos($string, $substring);
if($position == FALSE){echo "'$substring' doesn't exist in '$string'.". "<br />";}
if($position === FALSE){echo "'$substring' doesn't exist in '$string'.";}
else{echo "'$substring' exists at position $position in the '$string'.";}
?>

Output:

‘Learn’ doesn’t exist in ‘Learn PHP in-depth’.
‘Learn’ exists at position 0 in the ‘Learn PHP in-depth’.

Practical Usages of strpos() Function:

  • strpos() function is a fast and less memory intensive function to check if a substring exists in a string. Though it might not be the fastest way, but it can serve the purpose.
  • You can validate user supplied data with this function verifying whether certain words exist there.

Notes on strpos() Function:

  • The function is case sensitive. Which means it differentiates between uppercase and lowercase. So, it considers “a” and “A” are two different characters. Check example 5.
  • The function is binary-safe. This function considers its input as byte, it handles binary data (i.e. non-ASCII bytes, UTF-8) correctly. Check example 6.

Caution:

  • If the substring starts at the 0th position of the string, the function returns 0. With this output, if you check the substring’s existence with loose comparison operator (==), you won’t find this substring in the string, but it exists. It happens because the comparison operator considers FALSE and “0” have the same value.
    To overcome this, use strict comparison operator (===) which not only checks values but also checks type of the both value. As the function returns numeric 0, the strict comparison operator doesn’t consider this case as FALSE which is a Boolean type value. Check example 7.
  • Developers sometimes make mistake in parameter-order. They place substring before the string in the function and get unexpected result.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP () Function

strpos() is another very useful built-in string function to manipulation string. Knowing the correct usage of this function will help you to manipulate strings efficiently and accurately.

Reference:

https://www.php.net/manual/en/function.strpos.php