Categories
PHP

Extract text before or after the match

How to retrieve the portion of text from a string with the help of strstr(), stristr(), strrchr(), and strpbrk() function.

  1. strstr : returns a portion of the string from the searched keywords to the end of the string.
  2. stristr : works similar to the strstr() except it ignores the case.
  3. strchr : alias to strstr().
  4. strrchr : works similar to the strstr() except it finds the last occurrence of a character in a string.
  5. strpbrk : returns a portion of the string from the first matched character to the end of the string.

strstr()

<?php
 //Syntax
 strstr(string $haystack, string $needle,
        bool $before_needle = false
       ): string|false
  1. $haystack: string in which to search.
  2. $needle: text to find.
  3. $before_needle: Boolean value (default is false)

The strstr( ) search is case-sensitive. If the $needle isn’t found in the $haystack string, it returns false else returns the remainder of the $haystack string from where the first occurrence of the $needle begins. The following examples show how the functions work:

<?php
 $str = 'Welcome to BrainBell.com';
 echo strstr($str, 'to'); 
 // 'to BrainBell.com'

 $res = strstr($str, 'Hi');
 var_dump ($res);
 // bool(false)

By default, the strstr function returns the first occurrence of the matched text ($needle) + the remaining text of the string, as shown in the above example. If you want to retrieve the text before the first occurrence of the $needle, set true on the third parameter $before_needle:

<?php
 $str = 'Welcome to BrainBell.com';
 echo strstr($str, 'to', true); 
 // 'Welcome '

stristr()

The stristr() function works similar to the strstr() function except it makes the case-insensitive search.

<?php
 $str = 'Welcome to BrainBell.com';
 
 echo strstr($str, 'brain'); 
 //Prints nothing
 
 echo stristr($str, 'brain'); 
 // 'BrainBell.com'
 
 echo stristr($str, 'brain', true);
 // 'Welcome to '

strchr function

This function is an alias of strstr().

strrchr function

strrchr(string $haystack, string $needle): string|false

The strrchr() function searches backward in the string, but only for a single character. It returns FALSE if the searched character is not found, else returns the portion of $haystack from the last occurrence of $needle.

Note: This function searches for only a single character, if $needle contains more than one character only the first character is used. The following examples show how strrchr( ) works:

<?php
 $str = 'Welcome to BrainBell.com';
 echo strrchr($str, 'B'); 
 // 'Bell.com'
 
  echo strrchr($str, 'Brai'); 
 // 'Bell.com'
 
  echo strrchr($str, '.'); 
 // '.com'
 
  echo strrchr($str, ' '); 
 // ' BrainBell.com'

strpbrk()

<?php
 //Syntax
 strpbrk(string $string, string $characters): string|false

The strpbrk() takes two arguments: the $string string to search, and a string containing the list of characters ($characters) to search for. It returns the remainder of the $string from the first matched character to the end of the $string. The function returns FALSE if none of the characters in the second argument are found in the string. See the example:

<?php
 $string = 'Welcome to BrainBell';
 echo strpbrk ($string, 'aBc') .'<br>';
 // 'come to BrainBell'
 
 echo strpbrk ($string, 'aB');
 // 'BrainBell'

Manipulating substrings: