Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6731743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:33:55+00:00 2026-05-26T10:33:55+00:00

I need to find a sentence or sentences that is/are surrounding a string. That

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:33:56+00:00Added an answer on May 26, 2026 at 10:33 am

    Your search_string should be preg_quote’d, or users can manipulate the results with special characters like |

    $search_string='example';
    $regex = '/[A-Z][a-z ]*\b'.preg_quote($search_string,"/").'\b.*?(?:[.!?]|$)/i';
    preg_match_all($regex, $content, $matches);  
    

    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:

    $search_string='example';
    $regex = '/[^.!?\n]*\b'.preg_quote($search_string,"/").'\b[^.!?\n]*/i';
    preg_match_all($regex, $content, $matches);  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find/create an application that will create employee web usage reports from
Could find a similar thing for C here. I need to break a sentence
Need to find the timestamp for the first minute of the first day of
I need find a line matching a pattern like: A xx B xx, where
I'm in PHP. I'd like to find numbers in a sentence that start with
I need to run a sql script that is in a file from my
Here's a quickie for your RegEx wizards. I need a regular expression that will
I need to find all English words which can be formed from the letters
I have a long string and I need to find instances of '#!#'+some text+'#!#'
I need to find a way to take a sentence and remove all its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.