Home >>PHP String Functions >PHP strstr() Function
PHP strstr() function is used to search the first occurrence of a string inside another given string and displays some part of the latter starting from the first occurrence of the former in the latter. It is case-sensitive and binary-safe. The case-insensitive version of this function is stristr() function.
Syntax:
strstr( $string, $search, $before );
Parameter | Description |
---|---|
String | This is a required parameter. This parameter defines the string to search. |
Search | This is a required parameter. This parameter defines the string to search for. |
Before | This is an optional parameter. This parameter holds a boolean value which is by default "false". If it is "true", it returns the part of the string before the first occurrence of the search parameter. |
Here is an example of strstr() function in PHP:
<html> <body> <?php echo strstr("A B C D A B C D","C"); ?> </body> </html>
Here is an another example of strstr() function in PHP:
<html> <body> <?php echo strstr("A B C D E A B C D E.",67); ?> </body> </html>
Here is an another example of strstr() function in PHP with the TRUE value of $before parameter:
<html> <body> <?php echo strstr("A B C D E A B C D E.","E",true); //return string before the occurrence ?> </body> </html>