0
我有一长段从搜索查询中回来的文本。 我想显示文本的片段,而不是整个内容,并突出显示片段中的搜索查询。 我意识到决定要分片的文本的哪一部分可能会变得复杂,并且我希望得到关于如何做到这一点的任何想法?在php中生成代码片段
谢谢!
我有一长段从搜索查询中回来的文本。 我想显示文本的片段,而不是整个内容,并突出显示片段中的搜索查询。 我意识到决定要分片的文本的哪一部分可能会变得复杂,并且我希望得到关于如何做到这一点的任何想法?在php中生成代码片段
谢谢!
例如
$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]);
是否有任何HTML字符,或者它是纯文本? – 2010-04-08 22:02:06
*(相关)* http://stackoverflow.com/search?q=highlight+text+php – Gordon 2010-04-08 22:02:30