0

I have a long chunk of text that came back from a search query. I'd like to display a snippet of the text, not the entire thing, and highlight the search query within the snippet. I realize that decided what part of the text to slice can be complicated, and I was hoping for any ideas on how do to this?

Thanks!

2

1 Answer 1

1

for example

$text = "
I have a long chunk of text that came back from a search query. I'd like to display a
snippet of the text, not the entire thing, and highlight the search query within the
snippet. I realize that decided what part of the text to slice can be complicated, and
I was hoping for any ideas on how do to this?
";

$query = "the text";

preg_match("~\b.{0,20}$query.{0,20}\b~", $text, $m);
echo str_replace($query, "<strong>$query</strong>", $m[0]);
Sign up to request clarification or add additional context in comments.

Comments

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.