$text = "Internal tissue sealants and hemostasis are relatively underpenetrated, although their use has grown rapidly. In 2012
, internal tissue sealants were used in just over 3% of the procedures listed above. Healthy growth is expected throughout
the forecast period, driven by growing adoption in key procedures, such as neurological surgeries and thoracic operations;
increasing use as an adjunct to hemostasis; and broadening indications. The value of revenues per procedure is expected to
remain relatively flat, below $400. Sales are expected to reach $370 million in 2018. Exhibit 4-7 presents the market forecast for
external and internal tissue sealants.";
And I would like to use PHP to search through it based on a word and return a snippet of the sentence where it appears and show a result like this:
Search word: hemostasis
…Internal tissue sealants and hemostasis are relatively underpenetrated…
…use as an adjunct to hemostasis; and broadening indications…
Tokenize the string (split on ’ ')
Preg_grep on “~.“.$term.”.~”; this gives you the keys of the matching tokens.
foreach key found,
Get the implosion (on ’ ') of the array_slice from max($key - $words_before,0) for length $words_to_include.
Str_replace your term with <strong>$term</strong>.
This will give you full words instead of characters before and after. echo “…”.$result.“…” and you’ve got the output you wanted.
<?php
function Search($text, $search){
$arr = explode($search, $text);
$out = array();
for ($i = 0; $i < count($arr)-1; $i++) {
$out[] = "...".substr($arr[$i], -30)." ".$search." ".substr($arr[$i+1], 0, 30)."...";
}
return $out;
}
$text="Internal tissue sealants and hemostasis are relatively underpenetrated, although their use has grown rapidly. In 2012, internal tissue sealants were used in just over 3% of the procedures listed above. Healthy growth is expected throughout the forecast period, driven by growing adoption in key procedures, such as neurological surgeries and thoracic operations; increasing use as an adjunct to hemostasis; and broadening indications. The value of revenues per procedure is expected to remain relatively flat, below $400. Sales are expected to reach $370 million in 2018. Exhibit 4-7 presents the market forecast for external and internal tissue sealants.";
$search="hemostasis";
$return=(Search($text, $search));
$i=0; foreach($return as $row) { $i++; echo $i.") ".$row."<br>"; }
?>