Home » Php » php – Output text in between two words

php – Output text in between two words

Posted by: admin July 12, 2020 Leave a comment

Questions:

I would like to use PHP to input a certain text and the output should be the text in between two words. To clarify:

Input:

Lorem ipsum dolor sit amet

Output:

dolor sit
How to&Answers:
$str = 'Lorem ipsum dolor sit amet';
$word1 = 'ipsum';
$word2 = 'amet';
preg_match('/'.preg_quote($word1).'(.*?)'.preg_quote($word2).'/is', $str, $match);
// result would be in $match[1]