Dmitriy
Dmitriy

Reputation: 93

PHP sentence search

Is it possible to search a sentence by inputting first and last words?

Example: We have a sentence "Hello world this is an example and here is FIRST word, or the start of a sentence that I need to take from here, and here is the LAST word, what means that everything before this word is not needed".

What I want to get: "FIRST word, or the start of a sentence that I need to take from here, and here is the LAST".

Something like this, and I need it in the PHP.

My idea is to do this with some array, which starts saving words from first word until last word.

Upvotes: 1

Views: 350

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46910

Question lacks any effort but is kind of interesting. This is 1 boring old fashioned way

<?php

$string="Hello world this is an example and here is FIRST word,
or the start of a sentence that I need to take from here, 
and here is the LAST word, what means that everything 
before this word is not needed";

$first="FIRST";
$last="LAST";

$string=substr($string,strpos($string,$first));                 // Cut Left
$string=substr($string,0,strpos($string,$last)+strlen($last)); //  Cut Right

echo $string;

Output

FIRST word, or the start of a sentence that I need to take from here, 
and here is the LAST

But since you didnt put it any effort, dont expect an explanation and or a better way with regexes :)

Upvotes: 1

Related Questions