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????

4
  • Of course this is the issue. Does the db return every single time the same html ? Or could it vary? Commented Oct 30, 2011 at 3:21
  • more or less the same format, but may vary Commented Oct 30, 2011 at 3:23
  • 1
    I think a str_replace() would be enough, as long as you're dealing with plain html. Anyway, the regex expression mentioned seems overcomplicated comparing to it's subject. Commented Oct 30, 2011 at 3:24
  • Then regex is not your friend in this case. Commented Oct 30, 2011 at 3:25

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Not in that context, no. You can use it though, it depends on what you're trying to accomplish.
Using the code you have pasted, how can I limit the words echoed around the highlighted keyword?? This is important as the text is long..
Hi Yoda thanks for that, trying to run the code in codepad and get an error. what does the $matches variable do???
Codepad preg_match is limited for some reason, try it locally or in a webserver, it'll work.
0

You can just use PHP's strip_tags() function.

$text = strip_tags($row->content);

1 Comment

Hi Clive, I have stripped the text of tags, but the preg_replace still doesn't run on the content from the database. Any ideas???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.