How to get a substring between two strings in PHP?

To get a substring between two strings there are few popular ways to do so. Below the procedures are explained with the example.

Examples:

Input:$string="hey, How are you?"
If we need to extract the substring between 
"How" and "you" then the output should be are
 
Output:"Are"

Input:Hey, Welcome to GeeksforGeeks
If we need to get the substring between Welcome and 
for then it should be to geeks as for lies within GeeksforGeeks.

Output:" to geeks"

Method 1: Firstly you have to find the ending position of the start word. Then find the starting position of the ending word after that return the substring between those indexes.
Below program illustrate the approach:



Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    function string_between_two_string($str, $starting_word, $ending_word)
{
    $subtring_start = strpos($str, $starting_word);
    //Adding the strating index of the strating word to 
    //its length would give its ending index
    $subtring_start += strlen($starting_word);  
    //Length of our required sub string
    $size = strpos($str, $ending_word, $subtring_start) - $subtring_start;  
    // Return the substring from the index substring_start of length size 
    return substr($str, $subtring_start, $size);  
}
  
$str = 'Hey, Welcome to geeksforgeeks';
$substring = string_between_two_string($str, 'Welcome', 'for');
  
echo $substring;
      
?>

chevron_right


Output:

 to geeks

Method 2: Using the explode() function. The explode function splits the given string on the basis of the parameter provided i.e. the separator.
Syntax:

 $arr=explode(separator, string).

This will return an array which will contain the string split on the basis of the separator.

  • Split the list on the basis of the starting word.
  • In the returned array the 2nd index will contain the words after the start word.
  • In the string provided by step 2 if we again split it on the basis of the ending word then the value at first index will be the string between the starting and the ending word.

Below program illustrate the approach

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  function string_between_two_string($str, $starting_word, $ending_word){
    $arr = explode($starting_word, $str);
    if (isset($arr[1])){
        $arr = explode($ending_word, $arr[1]);
        return $arr[0];
    }
    return '';
  }
  
  $str = "Hey, how are you?";
  $start = "Hey";
  $end = "you";
  $substring = string_between_two_string($str, $start, $end);
  echo $substring;
?>

chevron_right


Output:

, how are 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.