Advertisement
Zweieck2

PHP string search algorithm

May 25th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | None | 0 0
  1. $INDEX = array('of'=>'long', 'strings that'=>'should', 'be searched for,'=>'transformed to lower case', 'and'=>'without double spaces'); # the values act as slug names e.g. for articles, the keys are the titles
  2. $search = 'search string, transformed to lower case and without double spaces';
  3. $res = array(); # empty array for results
  4.  
  5. foreach ($INDEX as $key => $val) {
  6.     $searchptr = 0;
  7.     while (TRUE) {
  8.         $str = $key;
  9.         $i = stripos($str, $search[$searchptr]);
  10.         if ($i === FALSE) {
  11.             break 1;
  12.         } else {
  13.             $str = substr($str, $i+1);
  14.             $i = 1; # now used to count matching letters = recognition length threshold (here at 3 chars)
  15.             if ($i === strlen($search)) {
  16.                 if (!array_key_exists($val, $res)) { # sorting slugs only, score = relevance of result
  17.                     $res[$val] = 0;
  18.                 }
  19.                 $res[$val]++;
  20.             }
  21.  
  22.             $searchptr++;
  23.             while (TRUE) {
  24.                 if ($searchptr >= strlen($search)) {
  25.                     break 2;
  26.                 } elseif (strlen($str) == 0) {
  27.                     break 1;
  28.                 } elseif ($str[0] !== $search[$searchptr]) {
  29.                     /* single mismatch, no score for you! */
  30.                     /* these conditions are 1 char fault tolerant */
  31.                     if ($str[1] === $search[$searchptr]) {
  32.                         # missing char in search
  33.                         $str = substr($str, 1);
  34.                     } elseif ($str[0] === $search[$searchptr+1]) {
  35.                         # missing char in string
  36.                         $searchptr += 2;
  37.                     } elseif ($str[1] === $search[$searchptr+1]) {
  38.                         # char mismatch
  39.                         $str = substr($str, 1);
  40.                         $searchptr += 2;
  41.                     } else {
  42.                         /* none of the next chars match; match has ended */
  43.                         break 1;
  44.                     }
  45.                 } else {
  46.                     /* another match! */
  47.                     $str = substr($str, 1);
  48.                     if (++$i > 2 || $i === strlen($search)) {
  49.                         if (!array_key_exists($val, $res)) { # sorting slugs only, score = relevance of result
  50.                             $res[$val] = 0;
  51.                         }
  52.                         $res[$val]++;
  53.                     }
  54.                     $searchptr++;
  55.                 }
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement