Reputation: 27
I have to fetch a complete sentence from the given string in which the desired character must exist. Like say I have a search Key "different topic"and i am searching it in "Our community is defined by a specific set of topics in the help center; please stick to those topics and avoid asking for opinions or open-ended discussion. If your question is about the site itself, ask on our meta-discussion site. If you’re looking for a different topic, it might be covered on another Stack Exchange site."
After my search I want it to return as complete sentence "*If you’re looking for a different topic, it might be covered on another Stack Exchange site.*"
This is the complete sentence containing my search key too.
I have used the following for this but not getting the proper output:
substr($fp, strpos($fp, $search_string) - 100, 200).
This is all in PHP.
Upvotes: 0
Views: 57
Reputation: 27
I resolved my issue with this following code:-
Instead of this:- substr($fp, strpos($fp, $search_string) - 100, 200);
I have used following to fetch the sentence with complete words containing my search key:-
$sentences = explode('.',$fp);
$subString = "";
foreach($sentences as $sentence) {
if(false !== stripos($sentence,$search_string)){
$subString = trim($sentence) . "\n";
}
}
and this helped me to fetch the proper result.
Upvotes: 0
Reputation: 34416
You really cannot do this using substr()
but you can with a little regex:
[A-Z][^\\.;]*(different topic)[^\\.;]*
Here is an EXAMPLE
If you need the period at the end of the sentence, modify the regex slightly to remove the period from the character match at the end of the phrase, [^\\;]
instead of [^\\.;]
[A-Z][^\\.;]*(different topic)[^\\;]*
Upvotes: 2