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 8758063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:27:08+00:00 2026-06-13T14:27:08+00:00

I am trying to do the following : grab 5 words before the search

  • 0

I am trying to do the following :

grab 5 words before the search phrase (or Y if there is only Y words there) and 5 words after the search phrase (or Y if there is only Y words there) from a block of text (when I say words I mean words or numbers whatever is in the block of text)

eg

The block of text: “Welcome to Stack Overflow! Visit your user page to set your name and email.”

if you was to search “visit your” it would return: “Welcome to Stack Overflow! Visit your user page to set your”

I’ve tried using this

$preg_safe = str_replace(" ", "\s", preg_quote($search)); 
$pattern = "/(\w*\S\s+){0,8}\S*\b($preg_safe)\b\S*(\s\S+){0,8}/ix";
if(preg_match_all($pattern, $full_text, $matches))
{ 
    $result = str_replace(strtolower($search), "<span class='searched-for'>$search</span>", strtolower($matches[0][0])); 
}
else
{ 
    $result = false; 
}

And it works if the search phrase is in English, but I need it to work in other languages as well. It doesn’t work for an Hebrew search phrase for example.

I’ve tried to change the pattern to :

$pattern = "(*UTF8)/(\w*\S\s+){0,8}\S*\b($preg_safe)\b\S*(\s\S+){0,8}/i";

But it didn’t work.

How can I make it work for other languages?

////////////////// EDIT //////////

As enrico.bacis suggested – I’ve changed the pattern to :

$pattern = "/(\w\p{Hebrew}*\S\s+){0,20}\S*\b($preg_safe)\b\S*(\s\S+){0,20}/ixu";

Now it works for English and Hebrew search phrases, but the result text is being cut when there is a special character (‘ for example).

How can I make the pattern return the text around the search phrase even if it contains special characters?

  • 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-06-13T14:27:09+00:00Added an answer on June 13, 2026 at 2:27 pm

    Your problem is on the \w that is not matching Hebrew characters, in fact \w is just a shortcut for a so-called “word” character: [A-Za-z0-9_].

    To make a regex able to capture also Hebrew characters you need only to make two changes:

    • Add u to the modifier to manage UTF8 characters (so your modifier will be /ixu)

    • Replace [\w\p{Hebrew}] for every occurrence of \w in your pattern.

    You can also check here for more answers on this topic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the following regular expression to grab only the letters from
I'm trying to grab the text from options in a dropdown list. Assume that
I'm trying to grab thousands of invoices (and other stuff) from a text file
I've got the following code in my Services project, which is trying to grab
I am trying following, 1. Get response using Ajax from the users selection which
I currently am trying the following code: $sql = $db->query(select id,username,fullname from userss ORDER
I am trying the following query and get only zero results (I am supposed
i'm trying the following MySQL query to fetch some data: SELECT m.*, t.* FROM
What I am trying to do with the following code is have it grab
I have the following regular expression: (?:<(?<tag>\w*)>(?<text>.*)</\k<tag>>) I want it t grab the text

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.