diff options
author | Thomas Bruederli <bruederli@kolabsys.com> | 2015-03-03 14:54:36 +0100 |
---|---|---|
committer | Thomas Bruederli <bruederli@kolabsys.com> | 2015-03-03 14:54:36 +0100 |
commit | c32998084d6d7995e7ef9a100a842b492f32b6f9 (patch) | |
tree | f1c0ba0a6d4c963fc6ea2e0f6eb050af75d63f2b /program/lib/Roundcube/rcube_utils.php | |
parent | 36ee2c8427298fc8735fe547d11c7e203fb3ca99 (diff) |
Add untility function to match strings ignoring word order
Diffstat (limited to 'program/lib/Roundcube/rcube_utils.php')
-rw-r--r-- | program/lib/Roundcube/rcube_utils.php | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/program/lib/Roundcube/rcube_utils.php b/program/lib/Roundcube/rcube_utils.php index f4c0e90ca..0ca2a9e31 100644 --- a/program/lib/Roundcube/rcube_utils.php +++ b/program/lib/Roundcube/rcube_utils.php @@ -917,7 +917,7 @@ class rcube_utils */ public static function tokenize_string($str, $minlen = 2) { - $expr = array('/[\s;\/+-]+/ui', '/(\d)[-.\s]+(\d)/u'); + $expr = array('/[\s;,"\'\/+-]+/ui', '/(\d)[-.\s]+(\d)/u'); $repl = array(' ', '\\1\\2'); if ($minlen > 1) { @@ -985,6 +985,28 @@ class rcube_utils } /** + * Compare two strings for matching words (order not relevant) + * + * @param string Haystack + * @param string Needle + * @return boolen True if match, False otherwise + */ + public static function words_match($haystack, $needle) + { + $a_needle = self::tokenize_string($needle, 1); + $haystack = join(" ", self::tokenize_string($haystack, 1)); + + $hits = 0; + foreach ($a_needle as $w) { + if (stripos($haystack, $w) !== false) { + $hits++; + } + } + + return $hits >= count($a_needle); + } + + /** * Parse commandline arguments into a hash array * * @param array $aliases Argument alias names |