返回带有高亮搜索文本和截断数据的搜索结果。

huangapple go评论67阅读模式
英文:

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(&quot;/{$search_text}/i&quot;, $model-&gt;content))
 {
    return preg_replace(&quot;/\w*?$search_text\w*/i&quot;, &quot;&lt;span style=\&quot;background-color:yellow\&quot; &gt;$0&lt;/span&gt;&quot;, $model-&gt;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(&quot;/{$search_text}/i&quot;, $model-&gt;content)) {
    // Highlight the search text
    $highlighted = preg_replace(&quot;/\w*?$search_text\w*/i&quot;, &quot;&lt;span style=\&quot;background-color:yellow\&quot; &gt;$0&lt;/span&gt;&quot;, $model-&gt;content);
    
    // Truncate the data
    if (strlen($highlighted) &gt; $truncated_length) {
        $highlighted = substr($highlighted, 0, $truncated_length) . &#39;...&#39;;
    }
    
    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(&quot;/{$search_text}/i&quot;, $model-&gt;content, $matches, PREG_OFFSET_CAPTURE)) {
    $match_offset = $matches[0][1];
    // Highlight the search text
    $highlighted = preg_replace(&quot;/\w*?$search_text\w*/i&quot;, &quot;&lt;span style=\&quot;background-color:yellow\&quot; &gt;$0&lt;/span&gt;&quot;, $model-&gt;content);
    
    // Truncate the data
    if (strlen($highlighted) &gt; $truncated_length) {
        $truncated = substr($highlighted, $match_offset, $truncated_length);
        if ($match_offset &gt; 0) {
            $truncated = &#39;...&#39; . $truncated;
            $truncated_length += 3;
        }
        if ($match_offset + $truncated_length &lt; strlen($highlighted)) {
            $truncated = $truncated . &#39;...&#39;;
        }
        $highlighted = $truncated;
    }
    
    return $highlighted;
}

return null;}

Regards

huangapple
  • 本文由 发表于 2023年4月6日 21:01:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定