stristr() function in PHPThe stristr() is an in-built function of PHP which is used to search the first occurrence of the string inside another string, and it returns rest of the string if the string is present. It is a binary-safe function (Binary safe function means - a function which can be performed on binary file without modifying the content of the file). The stristr() is a case-insensitive function which is similar to the strstr(). Both functions are used to search a string inside another string. The only difference between them is that stristr() is case-insensitive whereas strstr() is case-sensitive function. Note: This function is binary-safe and case-insensitive function. In stristr() function 'I' stands for insensitive.SyntaxThe syntax for the PHP stristr() function is as follows: Parameter$string (required): This parameter is a mandatory parameter which specifies the string which is to be searched, means it is the main string in which $search (discussed next) value is searched. $search (required): This parameter is also mandatory as $string. This parameter specifies the string which is going to be searched in $string. If this parameter is a number or integer value rather than a string, then it will use it as ASCII value. $before_search (optional): This parameter is an optional parameter which specifies the Boolean value whose default value is FALSE. If we set it to TRUE, then it will return the part of the string before the first occurrence of the search parameter. Return ValuesThe PHP stristr() returns the remaining string (starts from the matching point), or it will return FALSE, if the string which we are searching for is not found. Technical Details
ExamplesBelow are some examples through which you can learn the practical implementation of the stristr() function in the program. Input: $string = "Hello PHP! ", $search = "PHP "; Output: PHP! Input: $string = "Hello PHP! ", $search = "p ", before_search = true; //case-insensitive Output: Hello Input: $string = "Hello PHP! ", $search = "K ", before_search = true; Output: Following are some detailed examples which are given below - Example 1 It is the simple example of stristr() which shows that it is case-insensitive function and return the rest of the string where the $search variable will find. Output: Example 2 In this example, we use the third parameter, i.e., $before_search = true, which will return the part of the string before the first occurrence of the $search1 variable otherwise Boolean value false. Output: We used var_dump() function to print the false value because echo was not sufficient to print this Boolean value as you can see in the output while searching $search1 value. Example 3 In this example, we will pass an integer in $search rather than string or character. It treats that integer value as ASCII value. Output: Note: var_dump() function also print the number of character including whitespace and their return type. E.g. false is a Boolean value whereas in second line, number of character returned by the stristr() is a string. Echo does not display these information.Next TopicPHP String Functions |