mysql 字符串搜_Mysql搜索字符串

如果你想使用php ..几个月前我写了一个函数。它在文本中搜索关键字并返回带有“粗体”关键字的字符串。希望这可以帮助。

$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,"".$d."",$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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值