3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Similarity { protected $data = null; protected $distance = null; public function __construct($data, $distance) { $this->data = (string)$data; $this->distance = (int)$distance; } public function checkMatch($search, callable $checker=null, array $args=[], $return=false) { $solid = $this->normalizeInput($this->data, $this->search); $data = $solid['data']; $search = $solid['search']; $data = preg_split('/\s+/', $data); foreach($this->getAssoc($data, substr_count($search, ' ')+1) as $assoc) { foreach($this->getPermutations($assoc) as $ordered) { $ordered = join(' ', $ordered); $result = call_user_func_array($checker, array_merge([$ordered, $search], $args)); if($result<=$this->distance) { return $return?$ordered:true; } } } return $return?null:false; } protected function getPermutations(array $input) { if(count($input)==1) { return [$input]; } $result = []; foreach($input as $key=>$element) { foreach($this->getPermutations(array_diff_key($input, [$key=>0])) as $subarray) { $result[] = array_merge([$element], $subarray); } } return $result; } protected function nextAssoc($assoc) { if(false !== ($pos = strrpos($assoc, '01'))) { $assoc[$pos] = '1'; $assoc[$pos+1] = '0'; return substr($assoc, 0, $pos+2). str_repeat('0', substr_count(substr($assoc, $pos+2), '0')). str_repeat('1', substr_count(substr($assoc, $pos+2), '1')); } return false; } protected function getAssoc(array $data, $count=2) { if(count($data)<$count) { return null; } $assoc = str_repeat('0', count($data)-$count).str_repeat('1', $count); $result = []; do { $result[]=array_intersect_key($data, array_filter(str_split($assoc))); } while($assoc=$this->nextAssoc($assoc)); return $result; } protected function normalizeInput($data, $search) { $data = preg_split('/\s+/', trim(strtolower($data)), -1, PREG_SPLIT_NO_EMPTY); $search = preg_split('/\s+/', trim(strtolower($search)), -1, PREG_SPLIT_NO_EMPTY); if(count($data)<count($search)) { $temp = $data; $data = join(' ', $search); $search = join(' ', $temp); } else { $data = join(' ', $data); $search = join(' ', $search); } return ['data'=>$data, 'search'=>$search]; } } $data = 'Louise Bro'; $search = [ 'Louise Gulbæk Bro', 'Niels Faurskov', 'Niels Faurskov Andersen', 'Nils Faurskov Andersen', 'Nils Andersen', 'niels faurskov', 'niels Fæurskov', 'niffddels Faurskævffre', 'jens grunnet' ]; $checker = new Similarity($data, 2); echo(sprintf('Testing "%s"'.PHP_EOL.PHP_EOL, $data)); foreach($search as $name) { echo(sprintf( 'Name "%s" has %s'.PHP_EOL, $name, ($result=$checker->checkMatch($name, 'levenshtein', [], 1)) ?sprintf('matched with "%s"', $result) :'mismatched' ) ); }
Output for 5.4.0 - 7.1.0
Testing "Louise Bro" Notice: Undefined property: Similarity::$search in /in/vuLAk on line 15 Name "Louise Gulbæk Bro" has mismatched Notice: Undefined property: Similarity::$search in /in/vuLAk on line 15 Name "Niels Faurskov" has mismatched Notice: Undefined property: Similarity::$search in /in/vuLAk on line 15 Name "Niels Faurskov Andersen" has mismatched Notice: Undefined property: Similarity::$search in /in/vuLAk on line 15 Name "Nils Faurskov Andersen" has mismatched Notice: Undefined property: Similarity::$search in /in/vuLAk on line 15 Name "Nils Andersen" has mismatched Notice: Undefined property: Similarity::$search in /in/vuLAk on line 15 Name "niels faurskov" has mismatched Notice: Undefined property: Similarity::$search in /in/vuLAk on line 15 Name "niels Fæurskov" has mismatched Notice: Undefined property: Similarity::$search in /in/vuLAk on line 15 Name "niffddels Faurskævffre" has mismatched Notice: Undefined property: Similarity::$search in /in/vuLAk on line 15 Name "jens grunnet" has mismatched
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/vuLAk on line 13
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/vuLAk on line 13
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vuLAk on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vuLAk on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/vuLAk on line 4
Process exited with code 255.