Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output text in between two words

Tags:

php

search

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
like image 291
Deniz Zoeteman Avatar asked Feb 26 '11 14:02

Deniz Zoeteman


People also ask

How do I get text between two words in Excel?

In the Cell box, select a cell where you want to extract the text (here I select cell B5); In the Start char(s) box, enter the start word of the two words you want to extract all text strings after it; In the End char(s) box, enter the end word of the two words you want to extract all text strings before it.

How do I extract text between two words in Unix?

How do I extract text between two words ( <PRE> and </PRE> ) in unix or linux using grep command? Let us see how use the grep command or egrep command to extract data between two words or strings. I also suggest exploring sed/awk/perl commands to extract text between two words on your Linux or Unix machine.


1 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]
like image 196
delphist Avatar answered Oct 16 '22 01:10

delphist