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 array_pop() Function

PHP array_pop() Function The array_pop() function in PHP pops or removes the last element from the array shortening the array by one element. Syntax array_pop ( array &$array ) Parameter array(required)- This parameter represents the input array to get the value...

1 minute read.

PHP sort() Function

PHP sort() Function The sort() function in PHP sorts an array where the elements will be arranged from lowest to highest. Syntax sort ( 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...

1 minute read.

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP is_long() Function

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

1 minute read.

PHP echo() Function

PHP echo() Function The echo() function in PHP outputs or displays one or more strings. The major differences between print() function and echo() function is that echo accepts an argument list and doesn't have a return value. Also, it is slightly...

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 var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

PHP sha1() Function

PHP sha1() Function The sha1() in PHP calculates the SHA-1 hash of a string. Syntax sha1 ( string $str [, bool $raw_output = FALSE ] )  Parameter str (required)- This parameter represents the input string. raw_output(optional)- This parameter takes Boolean value and returns the digest in raw binary format...

1 minute read.

PHP pos() Function

PHP pos() Function The pos() function in PHP returns the current element in an array which is initialized to the first element of the array. This function is an alias of current(). Syntax pos ( array...

1 minute read.

PHP printf() Function

PHP printf() Function The printf() function in PHP outputs a formatted string. The related functions of printf() function are as follows: sprintf()vprintf()vsprintf()fprintf()vfprintf() Syntax printf ( string $format [, mixed $... ] )  Parameter format(required)- This parameter formats the string which is composed of zero or...

1 minute read.

PHP sizeof() Function

PHP sizeof() Function The sizeof() function in counts all elements and return the size of the array. This function is an alias of count(). Syntax sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)-...

1 minute read.

PHP natcasesort() Function

PHP natcasesort() Function The natcasesort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natcasesort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean...

1 minute read.

PHP get_defined_vars() Function

PHP get_defined_vars() Function The get_defined_vars() function in PHP returns an array of all defined variables. It returns a multidimensional array containing a list of all defined variables. Syntax get_defined_vars ( void ) Parameter NA Return This function returns a multidimensional array containing a list...

1 minute read.

PHP array_column() Function

PHP array_column() Function The array_column() function in PHP is used to return the values from a single column specified in the input array. Syntax array_column (array $input, mixed $column_key [, mixed $index_key]) Parameter input(required)- This parameter represents a multi-dimensional array or an array of...

1 minute read.

PHP array_key_exists() function

The array_key_exists () function in PHP is used to check whether the specified key exists in the array or not. It returns a Boolean value TRUE if the given key exists else it returns FALSE. Syntax array_key_exists(mixed key,array$array) Parameter key(required)- This...

1 minute read.

PHP htmlspecialchars_decode() Function

PHP htmlspecialchars_decode() Function The htmlspecialchars_decode() function in PHP convert some predefined HTML entities to characters. The some predefined HTML entities that will be decoded are as follows: &amp; OR & (ampersand)&quot; OR " (double quote)&#039;...

2 minutes read.

PHP strspn() Function

PHP strspn() Function The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter. Syntax strspn(string$subject,string$mask[,int$start[,int$length]] ) Parameter subject(required)- This parameter represents the input string. mask(required)- This parameter specifies the characters to find. start (optional)- It signifies the position in the string to start...

1 minute read.

PHP For loop with Example

For loop in PHP : The for loop is the most potent loop, it combines the abilities to set up variables as we enter the loop, test for conditions while iterating...

3 minutes read.

PHP krsort() Function

PHP krsort() Function The krsort() function in PHP sorts an array by key in reverse order while maintaining the keys. Syntax krsort (array & $array[,int $sort_flags= SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This...

3 minutes read.