du512926 2011-12-16 04:28
浏览 141
已采纳

Mysql搜索字符串

I have a search box that will search for a keyword in the field : description then return results. its done in mysql and php. the description can go from 10 to 600 characters long. I would like to return only a part of result. for example : I am searching for the keyword= John instead of displaying the whole field description as the result I want to see the sentence where the keyword is found only: ... Hello my name is john and I ... not the whole paragraph.

I can use sql or php whichever one is better. I tried substring but failed implementing it any example would be great.

SELECT  SUBSTR(description,2,100)  
FROM screenplays 
WHERE lower(description) like '% $search_word_fix %'
  • 写回答

4条回答 默认 最新

  • dongwen9947 2011-12-16 04:42
    关注

    if you want to use php.. I had written a function few months back. It searches for keywords in a text and returns a string with 'bold' keywords. Hope this helps.

        $str = 'I have a search box that will search for a keyword in the field : description then return results. its done in mysql and php. the description can go from 10 to 600 characters long. I would like to return only a part of result. for example : I am searching for the keyword= John instead of displaying the whole field description as the result I want to see the sentence where the keyword is found only: ... Hello my name is john and I ... not the whole paragraph.
    
        I can use sql or php whichever one is better. I tried substring but failed implementing it any example would be great.';
        $search = array();
        $search[] = "example";
        echo descriptionSearch($str,$search);
        function descriptionSearch($desc,$words,$max_number_words = 30)
        {
                    $descs = explode(" ",$desc);
                    $positions = array();
                    $i = 0;
                    foreach($words as $w)
                    {
                        $j = 0;
                        foreach($descs as $d)
                        {
                            $w = strtolower(trim($w));
                            $d = strtolower(trim($d));
                            if(strpos($d,$w) !== false)
                            {
                                $positions[] = $j;
                                $descs[$j] = str_replace($w,"<span style='font-weight: bold;'>".$d."</span>",$d);
                            }
                    $j++;
                }
                $i++;
            }
            $max = 0;
            if(sizeof($positions) > 0)
            {
                foreach($positions as $j)
                {
                    if($max < 4)
                    {
                        $i = $j -5;
                        if($i < 0)
                        {
                            $i = 0;
                        }
                        while($i < ($j+10))
                        {
                            $toreturn .= $descs[$i]." ";
                            $i++;
                        }
                        $toreturn .=  " ...";
                        $max++;
                    }
                }
            }
            else
            {
                for($i=0; $i < $max_number_words; $i++)
                {
                    $toreturn .=  $descs[$i]." ";
                }
            }
            return $toreturn;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画