search

Home  >  Q&A  >  body text

PHP full text search functionality using AND, OR and NOT operators

<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粉076987386P粉076987386473 days ago581

reply all(1)I'll reply

  • P粉218361972

    P粉2183619722023-09-06 11:48:46

    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? :-)

    reply
    0
  • Cancelreply