2010-04-08 4 views
0

검색 쿼리에서 나온 텍스트가 길다. 전체 내용이 아닌 텍스트 스 니펫을 표시하고 스 니펫 내에서 검색 쿼리를 강조 표시하고 싶습니다. 슬라이스 할 텍스트의 부분이 복잡 할 수 있다는 것을 알았습니다. 어떻게해야 할 지에 대한 아이디어가 있습니까?PHP 코드 단편 생성

감사합니다. 예를 들어

+0

는 HTML 문자가 있습니까 아니면 일반 텍스트입니까? –

+0

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

답변

1

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