floor9design/search-string-parser

A php based parser class for use in search logic.

0.9.1 2015-01-05 16:08 UTC

This package is auto-updated.

Last update: 2024-11-15 00:42:01 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

A php based parser that will turn a string into an array of search terms for usage in search logic. ** This software is currently in a beta release: 0.9.3 **

It is to be released on packagist.org.

Items left to look at:

  • Finish ParserEn implementation - currently under development
  • Testing has been restored to 100% on files not being redeveloped
  • Documentation to be expanded to be more "ELI5"

This should be PSR-2, PSR-4 compliant, but as it's a beta there may be some problems.

Install

Via Composer

composer require league/search-string-parser

Usage

There are different engines Parsers available. You can easily choose which you want to use. Currently in this Beta version ParserSimple and ParserEn are included.

Instantiate the wrapper using an object of the type of search you want:

// Instantiate 
$ssp = new SearchStringParser(new ParserSimple);
// run a search: 
$string = 'some search terms "including literals enclosed in quotes"';
$array = $ssp->parse($string);

This will give you the following results set:

array(
    0 => 'including literals enclosed in quotes',
    1 => 'some',
    2 => 'search',
    3 => 'terms'
);

Spaces are treated as "OR", with literals meaning "This string exactly"

  • hello world => hello OR world
  • "hello world" => hello AND [ space ] AND world

Your search string can include a combination of the following items:

  • strings - eg: hello world
  • literal string - eg: "hello world"
  • int - eg: 1
  • float - eg: -14.54
  • array
  • multi-dimensional array

These are all parsed internally to form an array ready for you to use as you see fit!

Here is a more complex example:

// Instantiate 
$ssp = new SearchStringParser(new ParserSimple);
// run a search: 
$complex = array(
            0 => 'hello world',
            1 => 'foo',
            2 => 'bar "literal string"',
            3 => 3,
            4 => -19.27
        );
$array = $ssp->parse($complex);

This gives the following result:

array(
    0 => 'literal string',
    1 => 'hello',
    2 => 'world',
    3 => 'foo',
    4 => 'bar',
    5 => '3',
    6 => '-19.27'
);

You can also change the delimiter:

// Instantiate 
$ssp = new SearchStringParser(new ParserSimple);
// run a search: 
$string = 'some "search terms" Pincluding literals enclosed in another letterP';
$array = $ssp->parse($string);

This will give you the following results set:

array(
    0 => 'including literals enclosed in another letter',
    1 => 'some',
    2 => '"search',
    3 => 'terms"'
);

A practical use case; In Zend Framework you may need to set up a set of search terms:

// we are in the middle of the code, and the object has already been instantiated as above:
 $string = 'some input text "including literals enclosed in quotes"';
$array = $ssp->parse($string);
 // ... I'll now skip to the relevant part of zend - I assume you can write a query:
 foreach($array as $keyword) {
   $select->orWhere('data_table LIKE ?', "%$keyword%")
}

Obviously, this is just a push in the right direction, though similar things can be done in almost all frameworks (as well as in raw SQL).

Testing

Ensure that you have run composer scripts to generate vendor/autoload.php Run the following in command line at the project root:

$ phpunit --bootstrap vendor/autoload.php tests/

Credits

License

GNU GENERAL PUBLIC LICENSE. Please see License File for more information.