定制 floor9design/search-string-parser 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

floor9design/search-string-parser

Composer 安装命令:

composer require floor9design/search-string-parser

包简介

A php based parser class for use in search logic.

README 文档

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.

统计信息

  • 总下载量: 147
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GNU
  • 更新时间: 2016-08-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固