3v4l.org

run code in 300+ PHP versions simultaneously
<?php $thisbatch = 1336; $description = 'Betalingskenmerk: 0815-69-AA-700' . "\n" . 'Transactiereferentie:'; $id = 1337; $refer = isolatenew($description); if($refer['result']=='success'){ // Reconciliate by new reference die('SUCCESS' . __LINE__); } elseif ($refer['result']=='fail'){ die('FAIL' . __LINE__); } else { // isolatenew returned partial success $suggestionFound = identify($refer, $id); if ($suggestionFound) { echo "UPDATE bank.transactions SET matched='$thisbatch',matchresult='104 Suggestie mail gestuurd naar operations' WHERE id='$id'\n"; } else { echo "UPDATE bank.transactions SET matched='$thisbatch',matchresult='101 Geen betaalkenmerk in omschrijving' WHERE id='$id'\n"; } } function isolatenew($input){ preg_match('#\d{3,5}[-_./ ]{0,2}\d{1,3}[-_./ ]{0,2}[a-zA-Z]{1,3}[-_./ ]{0,2}\d{3,5}#', $input, $result); if (!$result) { // Try hard, go pro! preg_match('#\d{5,7}[a-zA-Z]{1,3}\d{3,5}#', preg_replace('/[^a-zA-Z0-9]/', '', $input), $result); } if (empty($result)) { return array('result' => 'fail'); } else { //sql voor ophalen transacties van afgelopen week // eerst vervangen we eventuele dubbele streepjes met een enkel streepje $string = str_replace('--', '-', $result[0]); $string = str_replace(array(' ', '.','_','/'), '-', $string); if(strlen($string)==15&&substr_count($string, '-')==3){ $parts = explode('-', $string); if(strlen($parts[0])==4&&strlen($parts[1])==2&&strlen($parts[2])==2&&strlen($parts[3])==4){ return array('result' => 'success', 'return' => array($string)); } } $string = str_replace('-', '', $string); preg_match('/[A-Za-z]{1,3}/', $string, $lettersMatch); if (count($lettersMatch) >= 1) { $letters = $lettersMatch[0]; } else { // "Should never really happen" trigger_error('Bankwire matching: this should never really happen!', E_USER_WARNING); } if(strlen($string)==12&&strlen($letters)==2){ $str = substr($string, 0, 4) . '-' . substr($string, 4,2). '-' . substr($string, 6,2). '-' . substr($string, 8,4); return array('result' => 'success', 'return' => array($str)); } else { //doe exploden op de letters kijken welke onderdelen te salvagen zijn $str = explode($letters, $string); $lettersarray = array(); if(strlen($letters)==1){ $lettersarray = array( $letters.'A', $letters.'B', $letters.'C', $letters.'D', $letters.'E', $letters.'F', $letters.'G', $letters.'H', $letters.'J', $letters.'K', $letters.'L', $letters.'M', $letters.'N', $letters.'P', $letters.'Q', $letters.'R', $letters.'S', $letters.'T', $letters.'U', $letters.'V', $letters.'W', $letters.'X', $letters.'Y', $letters.'Z', 'A'.$letters, 'B'.$letters, 'C'.$letters, 'D'.$letters, 'E'.$letters, 'F'.$letters, 'G'.$letters, 'H'.$letters, 'J'.$letters, 'K'.$letters, 'L'.$letters, 'M'.$letters, 'N'.$letters, 'P'.$letters, 'Q'.$letters, 'R'.$letters, 'S'.$letters, 'T'.$letters, 'U'.$letters, 'V'.$letters, 'W'.$letters, 'X'.$letters, 'Y'.$letters, 'Z'.$letters ); } else if (strlen($letters) == 2) { $lettersarray[] = $letters; } else if(strlen($letters)==3){ $lettersarray[substr($letters, 0, 2)] = substr($letters, 0, 2); $lettersarray[substr($letters, 1, 2)] = substr($letters, 1, 2); $lettersarray[substr($letters, 0, 1).substr($letters, 2, 1)] = substr($letters, 0, 1).substr($letters, 2, 1); } $return[] = substr($string, 0, 4) . '-' . substr($string, 4,2). '-' . substr($string, 6,2). '-' . substr($string, 8,4); foreach($lettersarray as $letter){ $return[] = substr($str[0], 0, 4).'-'.substr($str[0], -2).'-'.$letter.'-'.substr($str[1], 0, 4); $return[] = substr($str[0], 0, 4).'-'.substr($str[0], -2).'-'.$letter.'-'.substr($str[1], 1, 4); } if(strlen($str[0])==6&&strlen($str[1])==4){ return array('result' => 'p_success_3', 'return' => $return, 'original' => $input); } if(strlen($str[0])==6&&strlen($str[1])!=4){ return array('result' => 'p_success_4', 'return' => $return, 'original' => $input); } if(strlen($str[0])!=6&&strlen($str[1])==4){ return array('result' => 'p_success_1_2', 'return' => $return, 'original' => $input); } } } } function findFuzzyMatches ($needle, $haystack, $tolerance = 1) { /* Returns (fuzzy) matches between $needle and elements of $haystack. The selected matches are those having Levenshtein distance up to $tolerance. Give $tolerance = -1 to get all matches. Return type is an associative array: the keys are the Levenshtein distance (integer), the values are sub-arrays containing the elements of $haystack having that distance. E.g.: $needle = 'abc'; $haystack = array('abc', 'abd', 'abe', 'xyz'); returns: array( 0 => array('abc'), 1 => array('abd', 'abe'), 3 => array('xyz') ) */ $matches = array(); // key: distance, value: sub-array with candidates having that distance foreach ($haystack as $candidateKey => $candidate) { // NOTE: Slowness? Use levenshtein, which is the faster function of the two. //$distance = levenshtein($needle, $candidate); // Downside: character swaps cost 2 instead of 1. $distance = DamerauLevenshtein($needle, $candidate); if ($distance <= $tolerance || $tolerance == -1) { if (!array_key_exists($distance, $matches)) { $matches[$distance] = array(); } $matches[$distance][$candidateKey] = $candidate; } } return $matches; } function DamerauLevenshtein($str1, $str2) { $d = Array(); $lenStr1 = strlen($str1); $lenStr2 = strlen($str2); if ($lenStr1 == 0) { return $lenStr2; } if ($lenStr2 == 0) { return $lenStr1; } for ($i=0; $i <= $lenStr1; $i++) { $d[$i] = Array(); $d[$i][0] = $i; } for ($j=0; $j <= $lenStr2; $j++) { $d[0][$j] = $j; } for ($i=1; $i <= $lenStr1; $i++) { for ($j=1; $j <= $lenStr2; $j++) { $cost = substr($str1, $i - 1, 1) == substr($str2, $j - 1, 1) ? 0 : 1; $d[$i][$j] = min( $d[$i - 1][$j] + 1, // deletion $d[$i][$j - 1] + 1, // insertion $d[$i - 1][$j - 1] + $cost // substitution ); if ( $i > 1 && $j > 1 && substr($str1, $i - 1, 1) == substr($str2, $j - 2, 1) && substr($str1, $i - 2, 1) == substr($str2, $j - 1, 1) ) { $d[$i][$j] = min( $d[$i][$j], $d[$i - 2][$j - 2] + $cost // transposition ); } } } return $d[$lenStr1][$lenStr2]; } function identify($refer, $bankTransactionID){ /* Returns whether a mail suggestion was sent to operations. If not, no sensible suggestion was found and the transaction should be rolled back. */ $suggestionFound = false; $weekstransactions = array(1338 => '0815-69-AA-7007'); foreach($refer['return'] as $reference){ $match = findFuzzyMatches($reference, $weekstransactions, 2); // $match contains all matches grouped by distance. Every match inside such a distance-array is keyed by its original saleID. $matches[$reference] = $match; } $mailbody = 'Input: "'.$refer['original'].'"'."\r\n"; $mailbody .= 'bank.transaction.id: '.$bankTransactionID."\r\n\r\n"; foreach($refer['return'] as $reference){ $mailbody .= 'mogelijke reference waarop we hebben gezocht: ' . $reference."\r\n"; if (empty($matches[$reference])) { $mailbody .= 'Niet gevonden!' . "\r\n\r\n"; } else { foreach($matches[$reference] as $key => $possibles){ $mailbody .= 'waarschijnlijkheid: ' . $key."\r\n"; foreach($possibles as $saleID => $possible){ $mailbody .= 'saleID: ' . $saleID."\r\n"; $mailbody .= 'reference: ' . $possible."\r\n"; $suggestionFound = true; } $mailbody .= "\r\n"; } $mailbody .= "\r\n\r\n"; } } if ($suggestionFound) { //mail('steven@targetmedia.nl', 'Bankwire', $mailbody); print 'MAILING' . "\n\n" . $mailbody; } return $suggestionFound; }

preferences:
44.15 ms | 402 KiB | 5 Q