英文:
Return search results with highlighted search text and truncated data
问题
如何在我的数据库的新闻表中创建一个搜索功能,它将返回带有突出显示的搜索文本和截断数据的结果?
我创建了一个名为get_search_text($model,$search_text)
的函数,接受新闻模型和搜索文本。
function get_search_text($model, $search_text){
if (preg_match("/{$search_text}/i", $model->content)) {
return preg_replace("/\w*?$search_text\w*/i", "<span style=\"background-color:yellow\">$0</span>", $model->content);
}
}
它会返回找到的新闻的整个内容,并且会突出显示搜索文本。但现在,如何截断返回的内容,而不是整个新闻?只截断包含搜索文本第一次出现的文本。
英文:
How can I create a search function in the news table of my database that will return results with highlighted search text and truncated data?
I created function get_search_text($model,$search_text)
. accepts news model and serach text .
function get_search_text($model,$search_text){
if (preg_match("/{$search_text}/i", $model->content))
{
return preg_replace("/\w*?$search_text\w*/i", "<span style=\"background-color:yellow\" >$0</span>", $model->content);
}
}
It return whole content of finded news with highlighted search text . But now how can I return it truncated not whole news . Only truncated text which contain first occurance of searched text
答案1
得分: 1
你的函数看起来可以用于在新闻表的内容列中查找和突出显示搜索文本。要截断数据,你可以使用PHP中的substr函数来限制返回的字符数。这是更新后的函数版本,包括了截断功能:
function get_search_text($model, $search_text, $truncated_length = 100) {
// 在内容列中查找搜索文本
if (preg_match("/{$search_text}/i", $model->content)) {
// 突出显示搜索文本
$highlighted = preg_replace("/\w*?$search_text\w*/i", "<span style=\"background-color:yellow\">$0</span>", $model->content);
// 截断数据
if (strlen($highlighted) > $truncated_length) {
$highlighted = substr($highlighted, 0, $truncated_length) . '...';
}
return $highlighted;
}
return null;
}
这个函数版本包括了第三个可选参数$truncated_length,默认为100个字符。你可以更改此值以截断数据到所需的长度。如果在内容列中找到搜索文本,函数将返回截断和突出显示的文本,否则返回null。
这是一个更新后的版本,可能会解决你提到的问题:
function get_search_text($model, $search_text, $truncated_length = 100) {
// 在内容列中查找搜索文本
if (preg_match("/{$search_text}/i", $model->content, $matches, PREG_OFFSET_CAPTURE)) {
$match_offset = $matches[0][1];
// 突出显示搜索文本
$highlighted = preg_replace("/\w*?$search_text\w*/i", "<span style=\"background-color:yellow\">$0</span>", $model->content);
// 截断数据
if (strlen($highlighted) > $truncated_length) {
$truncated = substr($highlighted, $match_offset, $truncated_length);
if ($match_offset > 0) {
$truncated = '...' . $truncated;
$truncated_length += 3;
}
if ($match_offset + $truncated_length < strlen($highlighted)) {
$truncated = $truncated . '...';
}
$highlighted = $truncated;
}
return $highlighted;
}
return null;
}
祝一切顺利!
英文:
Your function looks like it will work for finding and highlighting the search text in the content column of the news table. To truncate the data, you can use the substr function in PHP to limit the number of characters returned. Here's an updated version of your function that includes truncation:
function get_search_text($model, $search_text, $truncated_length = 100) {
// Find the search text in the content column
if (preg_match("/{$search_text}/i", $model->content)) {
// Highlight the search text
$highlighted = preg_replace("/\w*?$search_text\w*/i", "<span style=\"background-color:yellow\" >$0</span>", $model->content);
// Truncate the data
if (strlen($highlighted) > $truncated_length) {
$highlighted = substr($highlighted, 0, $truncated_length) . '...';
}
return $highlighted;
}
return null; }
This version of the function includes a third optional parameter, $truncated_length, which defaults to 100 characters. You can change this value to truncate the data to the desired length. The function returns the truncated and highlighted text if the search text is found in the content column, and null otherwise.
Here is an updated version that might solve your mentioned issue:
function get_search_text($model, $search_text, $truncated_length = 100) {
// Find the search text in the content column
if (preg_match("/{$search_text}/i", $model->content, $matches, PREG_OFFSET_CAPTURE)) {
$match_offset = $matches[0][1];
// Highlight the search text
$highlighted = preg_replace("/\w*?$search_text\w*/i", "<span style=\"background-color:yellow\" >$0</span>", $model->content);
// Truncate the data
if (strlen($highlighted) > $truncated_length) {
$truncated = substr($highlighted, $match_offset, $truncated_length);
if ($match_offset > 0) {
$truncated = '...' . $truncated;
$truncated_length += 3;
}
if ($match_offset + $truncated_length < strlen($highlighted)) {
$truncated = $truncated . '...';
}
$highlighted = $truncated;
}
return $highlighted;
}
return null;}
Regards
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论