PHP full text search functionality using AND, OR and NOT operators
P粉076987386
P粉076987386 2023-09-05 15:06:32
0
1
506
<p>Newbie question: I'm trying to implement a full text search on a search string using php and eloquent. </p> <pre class="brush:php;toolbar:false;">$searchstring = " (victim suspect) crime -covid"; $searchresult = RssItem::whereRaw("MATCH (description) AGAINST ('$searchstring' IN BOOLEAN MODE)")->get();</pre> <p>I want my users to be able to set a search string like this: </p> <pre class="brush:php;toolbar:false;">$searchstring = "(victim OR suspect) AND crime NOT covid";</pre> <p>Is there an easy way to convert user input to a boolean full text search string for mysql in php? </p>
P粉076987386
P粉076987386

reply all(1)
P粉218361972

I've done it the following way:

$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();

But maybe there is a better way? :-)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template