Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

extracting a whole sentence

extracting a whole sentence from a given string.

4:35 pm on Jan 11, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:July 22, 2005
votes: 0

Assume that we have a long string, say a paragraph like:

"sentence1. sentence2. sentence3... This is the sentence containing keywords. sentence5. sentence6...."

I know how search and locate the keywords. My problem is to extract the complete sentence that contain my keywords. That is to say, I will search the string, find the keyword(s), and extract only the sentence with my keyword(s) in it.

Could you please help me if there is a way to do it? Even a good hint pointing to the right direction will be appreciated.

Thank you in advance.

5:02 pm on Jan 11, 2007 (gmt 0)

New User

10+ Year Member

joined:Jan 4, 2005
votes: 0

Well, you could try to go about it using a regular expression and preg_match. Something like /\.\s([A-Z]{1}.*?keyword.*?\.)\s/ (not tested) might work on well-formed sentences that are in the middle of a paragraph (i.e. not first and not last). For the best results you'd have work with a Natural Language Processing Toolkit though, specifically a sentence detector that's been trained on enough representative data.
7:25 pm on Jan 11, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:May 4, 2004
votes: 0

What about splitting into an array with .?

$arr = explode('.', $string);
for ($i=0; $i<count($arr); $i++)
if (strstr('keyword', $arr[$i])!==false)
// Do whatever you have to do


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members