Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search block of text for term(s). Extract sentence that has term in it 1

Status
Not open for further replies.

iteach2

Technical User
Sep 16, 2001
120
US
Hi Gurus,

I am attempting to write a function that will go through a block of text and extract the needle and from the haystack. But I want the text before and after the needle as well.

Code:
 function findTerms($term, $content)
{
strip_tags($content);
$pieces = explode(".", $content);
preg_match_all("/\b$term\b/i", "$pieces",$out);
print_r($out);
}


findTerms("sporting goods", "The year was 1800 and sporting goods were not even thought of. We all looked out and saw sporting goods. And then he said, I like peanut butter.");
 
how do you define 'before and after'? everything in that paragraph? everything in that sentence? everything in the sentence before and after?

some are easier to achieve than others.
 
I would like to get everything in that sentence or paragraph. Kinda like how google search results will bring back the term in a sentence underneath the search results.
 
to select everything in a sentence

Code:
function getSentence ($needle, $haystack){
 $pattern = "/(^|\.\s+)(.*?".$needle.".*?)\./i';
 preg_match_all($pattern, $haystack, $matches);
 $return = array();
 foreach ($matches as $match){
   $return[] = $match[2];
 }
 return $return;
}

and to select everything in a paragraph try this
Code:
function getPara ($needle, $haystack){
 $pattern = '/(^|\n)(.*?'.$needle.'.*?)(\n|$)/i';
 preg_match_all($pattern, $haystack, $matches){
 $return = array();
 foreach ($matches as $match){
  $return[] = $match[2];
 }
 return $return;
}

 
Thank you jpadie,

You are dangerous with those regular expressions!

I was getting a blank screen when I ran the function so I modified a bit below:

Code:
function getSentence ($needle, $haystack)
{

$haystack = strip_tags($haystack);

 $pattern = "/(^|\.\s+)(.*?".$needle.".*?)\./i";

 preg_match_all($pattern, $haystack, $matches);
 
for($i=0; $i<sizeOf($matches); $i++)
{
 
 echo $matches[0][$i];
 
 }
 
}

Thank you loads for you help. [2thumbsup]
 
well ... you'd use the functions as i posted them like this

Code:
$matchedParas = getPara('search term', $text);
print_r($matchedParas); //this will come back as an array.

as all matches are found (not just the first) i've returned an array rather than a string.

your fix will only ever print out the first match. to print from inside the function simply change this line
Code:
$return[] = $match[2];
to
Code:
echo $match[2];
 
Hello jpadie,

I was wondering what I would need to change in your function to just count a certain amount of words before the term and after the term. something more generic because some of my results don't have periods at all causing no results to come back. Thank you so much.
 
Hello jpadie,

I actually just used the getPara and applied a truncation function to it. Thank you so much once again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top