php

PHP strpos() Function

PHP has many built-in functions to search the particular substring or the position of the substring in another string. The strpos() function is used to find out the position of the first occurrence of the substring in another string. The position of the substring can be searched from the starting or end of the main string based on the argument values of the strpos() function. The syntax of this function is given below.

Syntax:

The syntax of the strpos() function is shown below.

int|false strpos (string $string , string $substring , int $offset=0)
  • The function’s first argument contains the main string value where the substring will be searched.
  • The function’s second argument contains the substring value that will be searched in the main string.
  • The third argument of the function contains the position value from where the searching will start. The of this argument can be positive or negative. The searching will start from the beginning of the main string if the value of this argument is positive. The searching will start from the end of the main string if the value of this argument is negative. The default value of this argument is 0.
  • The function returns an integer value as position if the substring exists in the main string.

strpos() Function Examples

The uses of the strpos() function have been shown in the next part of this tutorial by using multiple examples.

Example-1: Search the Position of the String in Another String

Create a PHP file with the following script that will search the position of the particular string in another string by using the strpos() function. The search value will be taken from the URL parameter. If the return value of the strpos() function is greater than or equal to zero and not equal to false, then the search string exists in the main string.

<?php

//Check the search value is given or not

if(isset($_GET['s']))

{

//Define the string value

$strdata = 'PHP is a server-side scripting language';

//Read the search value

$search = $_GET['s'];

//Read the position of the searching string

$pos = strpos($strdata, $search);

//Check the position value

if($pos >= 0 && $pos != False)

echo "The $search string exists at the position $pos";

else

echo "The $search string does not exist.";

}

else

echo "The search value is not given."

?>

Output:

The following output will appear after executing the above script if no URL parameter is given.

http://localhost/php/strpos1.php

The following output will appear after executing the above script if the value ‘server’ is given in the URL parameter.

http://localhost/php/strpos1.php?s=server

The following output will appear after executing the above script if the value ‘client’ is given in the URL parameter.

http://localhost/php/strpos1.php?s=client

Example-2: Search the Position of the String in Another String With the Offset Value

Create a PHP file with the following script that will search the position of the particular string in another string after the particular position by using the strpos() function. The search value will be taken from the URL parameter. If the return value of the strpos() function is greater than or equal to zero and not equal to false, then the search string exists in the main string.

<?php

//Check the search value is given or not

if(isset($_GET['s']))

{

//Define the string value

$strdata = 'PHP is a server-side scripting language';

//Read the search value

$search = $_GET['s'];

//Read the position of the searching string from the position 10

$pos = strpos($strdata, $search, 10);

//Check the position value

if($pos >= 0 && $pos != False)

echo "The $search string exists at the position $pos";

else

echo "The $search string does not exist.";

}

else

echo "The search value is not given."

?>

Output:

The following output will appear after executing the above script if the value ‘server’ is given in the URL parameter, the ‘server’ string exists before the position, 10.

http://localhost/php/strpos1.php?s=server

The following output will appear after executing the above script if the URL parameter’s value ‘script’ is given; the ‘script’ string exists after the position, 10.

http://localhost/php/strpos1.php?s=script

Example-3: Print the Output of the strpos() Using var_dump()

Create a PHP file with the following script that will print the output of three strpos() function by using the var_dump() function. The first strpos() function will return 0 that is integer. The second strpos() function will return 9 that is integer. The third strpos() function will return false that is Boolean.

<?php

//Define the string value

$strdata = 'PHP is a popular language';

//Read the position of the searching string that exists at the beginning

$pos1 = strpos($strdata, 'PHP');

//Read the position of the searching string that exists in the middle

$pos2 = strpos($strdata, 'popular');

//Read the position of the searching string that does not exist

$pos3 = strpos($strdata, 'PHP8');

//Print the outputs

echo "The first output is: ";

var_dump($pos1);

echo "<br/>The second output is: ";

var_dump($pos2);

echo "<br/>The third output is: ";

var_dump($pos3);

?>

Output:

The following output will appear after executing the above script.

Example-4: Using strpos() With the Function

Create a PHP file with the following script that will search the string by using a user-defined function, and the strpos() function has been used inside the function with the negative offset value. The Search_position() function has been called twice in the script with two different search values.

<?php

//Define the string value

$str = "Hello World";

//Declare the function to search the string position

function Search_position($searchVal, $stringData)

{

$pos = strpos($stringData, $searchVal, -6);

if ($pos > 0)

{

return "The $searchVal exists at the position. $pos<br/>";

}

else

{

return "The $searchVal does not exist in the string.<br/>";

}

}

//Define a search value

$src = "World";

//Call the function

echo Search_position($src, $str);

//Define another search value

$src = "Hello";

//Call the function

echo Search_position($src, $str);

?>

Output:

The following output will appear after executing the above script. The ‘Hello’ string has been searched using the negative offset value that indicates a position value after the ‘Hello’ in the main string.

Conclusion

Different ways of searching the position of a string in another string by using the strpos() function have been shown in the examples of this tutorial to help the PHP users use this function properly in their script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.