PHP stristr() Function

PHP stristr() Function

The stristr() function in PHP finds the first occurrence of a string and returns the matched substring. It is a case-insensitive function.

Syntax

stristr ( string $haystack , mixed $needle [, bool $before_needle ] )

Parameter

haystack(required)- This parameter represents the input string.

needle(required)- This parameter signifies the string to search for. If it is a number, it will search for the character matching the ASCII value of the number.

before_needle(optional)- This parameter takes a boolean and its value default is "false". If it is set to "true", it returns the part of the string before the first occurrence of the search parameter.

Return

This function returns all of haystack starting from and including the first occurrence of needle to the end. If the needle is not found, it returns FALSE.

Example 1

<?php
  // initializing the string value
  $email = 'abc@gmail.com';
  echo "Original string: ".$email;
  echo "\nAfter the stristr() function...\n";
  //searching for the first occurrence of "e" inside string "$email". 
  echo "New String: ".stristr($email, 'b'); // bc@gmail.com
?> 

Output

Original string: abc@gmail.com
After the stristr() function...
New String: bc@gmail.com 

Example 2

<?php
   // initializing the string value
   $email = 'abc@gmail.com';
   echo "Original string: ".$email;
   echo "\nAfter the stristr() function...\n";
   //searching for the first occurrence of "e" inside string "$email". 
   echo "Output: ".stristr($email, 97); // 97 = lowercase a
   // output abc@gmail.com
 ?> 

Output

Original string: abc@gmail.com
After the stristr() function...
abc@gmail.com 

Example 3

<?php
   // initializing the string value
   $email = 'abc@gmail.com';
   echo "Original string: ".$email;
   echo "\nAfter the stristr() function...\n";
   //searching for the first occurrence of "e" inside string "$email". 
   echo "Output: ".stristr($email, 'b', true); // set true will return the part of the string before b.
   // output a
 ?> 

Output

Original string: abc@gmail.com
After the stristr() function...
Output: a 

Example 4

<?php
   // initializing the string value
   $str = "Hello PHP";
   echo "Original string: ".$str;
   if(stristr($str, 'world') === FALSE) {
       echo"\n";
     echo 'Sorry! "world" not found in string'; 
   }
 ?> 

Output

Original string: Hello PHP
Sorry!"world" not found in string

Related Topics

PHP wordwrap() Function

PHP wordwrap() Function The wordwrap() Function wraps a string to a given number of characters using a string break character. Syntax wordwrap ( string $str [, int $width [, string $break  [, bool $cut  ]]] ) Parameter str(required)- This parameter specifies the input string.       width(optional)- This parameter represents the number of...

1 minute read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.

PHP Variables

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol. Rules for PHP Variables. Variable starts with $ sign. Variable name...

2 minutes read.

How to use Date and Time in PHP

HOW TO USE DATE AND TIME IN PHP: PHP time and date function  informs us about current date and time. it can be manipulated also. How time() function works: The time() function...

2 minutes read.

PHP array_diff_uassoc() Function

PHP array_diff_uassoc() Function The array_diff_uassoc () Function in PHP is used to compare the keys and values of two (or more) arrays and returns the differences (an array containing the entries from the first array that...

1 minute read.

PHP array_intersect_assoc() function

PHP array_intersect_assoc() function The array_intersect_assoc() function in PHP is used to compute the intersection of arrays with the help of an additional index check. It returns an array containing all the entries from array1 that are present...

1 minute read.

PHP chunk_split() Function

PHP chunk_split() Function The chunk_split() function in PHP splits a string into a series of smaller parts. Syntax chunk_split(string,length,end) Parameter string(required)- It specifies the string to split. length(optional)- This parameter represents a number that defines the...

1 minute read.

PHP next() Function

PHP next() Function The next() function in PHP advances or moves the internal array pointer one place forward and returns the next array value. Syntax next ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns the next array value...

1 minute read.

PHP is_null() Function

The is_null() function in PHP is used to  find whether the specified variable is a null or not. It returns a Boolean value true if the parameter var is null else it returns...

1 minute read.

PHP Explode() Function

PHP Explode The PHP explode( ) is used to break a string into an array. The PHP explode( ) allow us to break a string into a smaller text with each break...

1 minute read.

PHP break

What is Break in PHP? Inside the loop, the break statement is used to control the loop in preprocessor hypertext. PHP utilizes the break keyword to implement the break statement, which...

3 minutes read.

PHP uksort() Function

PHP uksort() Function The uksort() function in PHP sorts an array by keys using a user-defined comparison function. Syntax uksort ( array &$array , callable $key_compare_func ) Parameter array(required)- This parameter represents the input array. key_compare_func(required)- This parameter represents a string that describe a callable comparison function....

1 minute read.

PHP gettype() Function

PHP gettype() Function The gettype () function in PHP gets the type of the specified variable. Syntax gettype ( mixed $var ) Parameter var(required)- This parameter represents the variable being type checked. Return This function returns the type of the PHP variable specified by “var”...

1 minute read.

PHP arsort() function

PHP arsort() function The arsort() function in PHP sorts an array in the reverse order and maintains the index association Syntax arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the behavior...

2 minutes read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.

PHP sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP similar_text() Function

PHP similar_text() Function The similar_text() function in PHP calculates the similarity between two strings. Syntax similar_text ( string $first , string $second [, float &$percent ] ) Parameter first(required): This parameter represents the first string to be compared. second(required): This parameter represents the second string to be compared. percent(optional): This...

1 minute read.

PHP stristr() Function

PHP stristr() Function The stristr() function in PHP finds the first occurrence of a string and returns the matched substring. It is a case-insensitive function. Syntax stristr ( string $haystack , mixed $needle [, bool $before_needle ] ) Parameter haystack(required)- This parameter represents the input string. needle(required)- This parameter...

2 minutes read.

PHP Switch Statement with Example

PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax: switch(n) {             case1: statement if n=case1; break;             case2:             statement if n=case2; break;             case3:            ...

3 minutes read.

PHP strrchr() Function

PHP strrchr() Function The strrchr() function in PHP finds the last occurrence of a character in the specified string and returns the portion of haystack, which starts at the last occurrence of the needle and goes until the end of haystack....

1 minute read.