strpos() Function

This function helps us to find the position of the first occurrence of a string in another string.

Syntax

strpos(original_str, search_str, start_pos);

Parameters

Out of the three parameters specified in the syntax, two are mandatory and one is optional. The three parameters are described below:

  • original_str: This is a mandatory parameter that refers to the original string in which we need to search for the occurrence of the required string.
  • search_str: This is a mandatory parameter that refers to the string that we need to search.
  • start_pos: This is an optional parameter that refers to the position of the string from where the search must begin.

Example: This example illustrate the basic implementation of the strpos() Function in PHP.

PHP




<?php
   
// PHP code to search for a specific string's position
// first occurrence using strpos() case-sensitive function
function Search($search, $string)
{
    $position = strpos($string, $search, 5);
    if (is_numeric($position))
    {
        return "Found at position: " . $position;
    }
    else
    {
        return "Not Found";
    }
}
 
// Driver Code
$string = "Welcome to w3wiki";
$search = "Geeks";
echo Search($search, $string);
?>


Output:

Found at position: 11

Explain some string functions of PHP

In the programming world, a string is considered a data type, which in general is a sequence of multiple characters that can contain whitespaces, numbers, characters, and special symbols. For example, “Hello World!”, “ID-34#90” etc. PHP also allows single quotes(‘ ‘) for defining a string. Every programming language provides some in-built functions for the manipulation of strings. Some of the basic string functions provided by PHP are as follows:

Similar Reads

strlen() Function

It returns the length of the string i.e. the count of all the characters in the string including whitespace characters....

strrev() Function

...

trim(), ltrim(), rtrim(), and chop() Functions

It returns the reversed string of the given string....

strtoupper() and strtolower() Function

...

str_split() Function

It removes white spaces or other characters from the string. They have two parameters: one string and another charList, which is a list of characters that need to be omitted....

str_word_count() Function

...

strpos() Function

It returns the string after changing the cases of its letters....

str_replace() Function

...

ucwords() Function

It returns an array containing parts of the string....