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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:12:24+00:00 2026-05-26T11:12:24+00:00

I am using preg_replace to create a snippet of searched content on my site

  • 0

I am using preg_replace to create a snippet of searched content on my site and highlight the term that was searched (like google). It works when I use a static variable on my page but when I retrieve the data from the database it doesn’t. The code is as follows:

foreach ($searchcontent ->result() as $row)
{
$text = $row->content;
$keyword = 'investment'; 
$size = 165; 

$snippet = preg_replace(
    '/^.*?\s(.{0,'.$size.'})(\b'.$keyword.'\b)(.{0,'.$size.'})\s.*?$/',
    '...$1<strong>$2</strong>$3...',
    $text
);
echo $snippet;

  }

If I change the $text variable for some static content like:
"We are happy to consultant on your investments" it works. But when I get the data from the db it doesn’t.

Sample of text from database (contains html, may be the issue),

<h2>Investments<h2>
<hr />
<p>We are happy to consult on your investments</p>

How would one go about stripping the string of html tags?? and getting the preg_replace working????

  • 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-26T11:12:25+00:00Added an answer on May 26, 2026 at 11:12 am

    This works : http://codepad.org/Ve1dhj8q

    However, it would be better if you paste a real database result, since the problem might be there.

    Edit

    This is what you want. You can use preg_match() to capture the contents before ( $matches[1] ) and after ( $matches[2] ), and don’t forget to check if there was any match found ( using count() ). Only thing left is to add the previous posted code in order to highlight the words. $limit is the offset.

    $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent consectetur tempor egestas. Nam arcu augue, bibendum sed pulvinar at, posuere ut massa. Duis tristique suscipit rutrum. Curabitur eu est sed sem consequat consequat ut sed erat. Aliquam ac eros est. Nulla posuere dolor eu arcu tempus suscipit auctor velit condimentum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Maecenas sollicitudin diam ut neque laoreet vitae placerat tortor commodo. Aliquam vehicula aliquet arcu non molestie. Sed adipiscing faucibus faucibus. In eget velit non elit ullamcorper consequat.';
    
    $limit = 50;
    $keyword = 'tempus';
    
    
    preg_match('/(.{0,'.$limit.'})'.$keyword.'(.{0,'.$limit.'})/', $text, $matches);
    
    echo var_dump($matches);
    

    Example output

    array(3)
    {
      [0]=> string(106) " Aliquam ac eros est. Nulla posuere dolor eu arcu tempus suscipit auctor velit condimentum. Class aptent t"
    
      [1]=> string(50) " Aliquam ac eros est. Nulla posuere dolor eu arcu "
    
      [2]=> string(50) " suscipit auctor velit condimentum. Class aptent t"
    }
    

    Bonus

    If you for design / markup purposes need to limit the phrase to, lets say 100 words (example, in case your div container can only take a certain amount of characters in order to keep the layout balance), you can do this :

    $limit = 0;
    $total = 100;
    $keyword = 'yumm';
    
    $limit = (int) ($total - strlen($keyword)) / 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using preg_replace to create urls for modrewrite based paging links. I use: $nextURL
Currently I'm using $str = preg_replace(/\([^\)]+\)/, , $str); // Remove () and Content $str
I'd like to replace the string {{edit(3)}} with open3close using preg_replace
I am using preg_replace to remove some content from a string. What I am
I have some code I that although it works, its making my content erratic.
Using preg_replace() , I want to replace a string like this... aaabbbccc => aaa******
I'm using preg_replace() as in the following code. $p = preg_replace('/#[\d\w]+/', '<a href=http://'.$_SERVER['HTTP_HOST'].'/search/term:'.str_replace('#', rawurlencode('%23'),
is it possible to create custom tag using php like i am trying to
I am using preg_replace to add a link to keywords if they are found
I'm trying to minify multiple CSS files using preg_replace() . Actually, I'm only trying

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.