新手问题: 我正在尝试使用php和eloquent在搜索字符串上实现全文搜索。
$searchstring = "+(victim suspect) +crime -covid";
$searchresult = RssItem::whereRaw("MATCH (description) AGAINST ('$searchstring' IN BOOLEAN MODE)")->get();
我希望我的用户能够设置一个类似这样的搜索字符串:
$searchstring = "(victim OR suspect) AND crime NOT covid";
有没有一种简单的方法将用户输入转换为php中mysql的布尔全文搜索字符串?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我已经通过以下方式完成了它:
$searchstring = '(victim OR suspect) AND crime NOT covid'; $searchstring = str_replace(" OR ", ' ', $searchstring); $searchstring = str_replace(" AND ", ' +', $searchstring); $searchstring = str_replace(" NOT ", ' -', $searchstring); $searchstring = '+'.$searchstring; $searchresult = RssItem::whereRaw("MATCH (description) AGAINST ('$searchstring' IN BOOLEAN MODE)")->get();但也许有更好的方法? :-)