I need to find a sentence or sentences that is/are surrounding a string. That will be from the first capital letter or break line to the end point or break line.
What I got is this but of course is not working at all:
$search_string='example';
$regex = '\[A-Z]{1}[a-z]*\s*'.$search_string.'\s*[a-zA-Z]*\i';
preg_match_all($regex, $content, $matches);
If the word is repeated in more than on sentence i will need to retrieve both sentences. I’m not sure if im explaining it well; please comment and I will try to explain it again.
EDIT
I have a wordpress website with lot of post and pdf, docs, etc inside those post. Im using a searchengine called swish-e to index all and display results.
When someone search for any string i want to display a summary of that string instead of the full post/ or pdf.
So if a user searchs “example” string, i need to show all the sentences or at least a few of them where the word example appears.
That´s why i asked for a capital letter at beggining and the end point at the end. I know this wont be perfect but at least i need to cover some scenarios (Capital letter / break lines, etc)
Hope its more clear, once again thanks a lot
Your search_string should be preg_quote’d, or users can manipulate the results with special characters like |
I’ve assumed the sentence can be terminated by . or ? or !
You probably don’t want to use \ characters for your pattern delimiters – if it works at all, it is likely to give odd behaviour. You also have the i pattern modifier applied to your pattern, so [a-z] will also match capital letters, and [A-Z] will match lower case chars.
Edit:
This solution is more flexible, though it doesn’t require the sentence to start with a capital letter. Up to you if you want to use it: