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: