Fulltext searching array of strings
I have a PHP array of strings: ie: "Big green car parked outside"..etc
I would like to perform boolean search operations on these strings, similar to MySQL fulltext searching , or Sphinx Searching.
For exam开发者_开发百科ple, I would like to find all strings containing word "green" but not "car"
Does anyone know of any existing PHP classes or libraries which would help me accomplish this ? Or can anyone suggest any google terms I could search for ?
Thank you in advance!
The PHP function array_filter()
will do the trick here.
function greennotcar($v)
{
if(strpos($v, " green ") === false)
return false
if(strpos($v, " car ") !== false)
return false
return true
}
$newarray = array_filter($array, "greennotcar");
This checks if the string contains green
, if it doesn't, exclude it from the array. Then, it checks if it contains car
, if it does, exclude it. Otherwise, include it.
EDIT: I added spaces in the strings so you wouldn't match cart as well as car, for example. This is all up to your own preference.
double preg_match()
function may be help you?
I don't know any existing standalone solution that can work directly with variables. You could code your own implementation using PCRE, str_word_count and other string/array functions. The resources are there. Inspiration may come as well from the approaches taken in some open source apps, like Dokuwiki's full-text search functions.
A more powerful solution (including an indexing process) could be Zend_Search_Lucene:
Zend_Search_Lucene is a general purpose text search engine written entirely in PHP 5. Since it stores its index on the filesystem and does not require a database server, it can add search capabilities to almost any PHP-driven website. Zend_Search_Lucene supports the following features:
- Ranked searching - best results returned first
- Many powerful query types: phrase queries, boolean queries, wildcard queries, proximity queries, range queries and many others.
- Search by specific field (e.g., title, author, contents)
精彩评论