2010-04-08 5 views
0

J'ai un long morceau de texte qui est revenu d'une requête de recherche. Je voudrais afficher un extrait du texte, pas le tout, et mettre en surbrillance la requête de recherche dans l'extrait. Je me rends compte que la partie du texte à découper peut être compliquée, et j'espérais avoir des idées sur la façon de faire cela?génération d'extraits en php

Merci!

+0

Y a-t-il des caractères HTML ou il est le texte brut? –

+0

* (apparenté) * http://stackoverflow.com/search?q=highlight+text+php – Gordon

Répondre

1

par exemple

$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]); 
Questions connexes