3v4l.org

run code in 300+ PHP versions simultaneously
<?php $companies = array( array('id' => '1','name' => 'Fifo Limited'), array('id' => '2','name' => 'FIFO Ltd'), array('id' => '3','name' => 'BitBytes Ltd'), array('id' => '4','name' => 'Info Sys'), array('id' => '5','name' => 'DevSquad'), array('id' => '6','name' => 'IT Magnet'), // 2/9 = 22% < 30% array('id' => '7','name' => 'Fifo-IT'), array('id' => '8','name' => 'Others'), array('id' => '9','name' => 'IT M2'), // 2/4 = 50% //array('id' => '10','name' => 'IT M3'), // 2/4 = 50% ); $percentage = 0.3; function getSimilarities($data, $pers){ $result = []; $tmp = []; foreach($data as $ind => &$record){ // split string by special chars '-' or ' ', array $name = preg_split('/[- ]/',$record['name']); // makes first word to lowercase $record['lowercase'] = strtolower($name[0]); // first word length $record['strlength_first'] = strlen($record['lowercase']); // string length $record['strlength_full'] = strlen($record['name']); // if length > 0 if ($record['strlength_full'] && // and if length of forst word > 30% string length $record['strlength_first']/$record['strlength_full'] >= $pers){ $input = preg_quote($record['lowercase'], '~'); if(preg_grep('~'.$input.'~', $record)) $tmp[$record['lowercase']][] = $record['id']; } } // if more than 1 = similar names foreach($tmp as $key=>$matches){ if (count($matches)>1){ $result[$key] = $matches; } } return $result; } print_r(getSimilarities($companies, $percentage));

preferences:
25.8 ms | 408 KiB | 5 Q