The strstr() function is a built-in function in PHP. It searches for the first occurrence of a string inside another string and displays the portion of the latter starting from the first occurrence of the former in the latter (before if specified). This function is case-sensitive.
strstr( $string, $search, $before )
Parameters : This function accepts three parameters as shown in the above syntax out of which the first two parameters must be supplied and the third one is optional. All of these parameters are described below:
- $string : It is a mandatory parameter which specifies the string in which we want to perform the search.
- $search : It is a mandatory parameter which specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number
- $before : It is an optional parameter. It specifies a boolean value whose default is false. If set to true, it returns the part of the $string before the first occurrence of the $search parameter.
Return Value : The function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.
Input : $string = "Hello world!", $search = "world" Output : world! Input : $string = "Geeks for Geeks!", $search = "k" Output : ks for Geeks!
Below programs illustrate the strstr() function in PHP :
Program 1: In this program we will display the portion of $string from the first occurrence of $search.
ks for Geeks!
Program 2: In this program we will display the portion of $string before the first occurrence of $search.
Program 3: In this program we will pass an integer as $search.
- How to get the function name inside a function in PHP ?
- D3.js | d3.sum() function
- PHP | sin( ) Function
- PHP | pow( ) Function
- p5.js | sq() function
- D3.js | d3.set.has() Function
- p5.js | nfp() Function
- D3.js | d3.mean() function
- PHP | ord() Function
- D3.js | d3.min() function
- PHP | cos( ) Function
- p5.js | abs() function
- p5.js | arc() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.