The issue with your code is that $resultat is a multidimensional array with a single value. The array_unique is called on the wrong value; a $resultat = $resultat[0]; would fix this, but the performance can be improved by taking a different approach.
PHP uses associative arrays. Store the lines as keys rather than values:
PHP Code:
function getCodes($fname) {
$codes = array();
$codeFile = fopen($fname, 'r');
if ($codeFile) {
while (FALSE !== ($line = fgets($codeFile))) {
preg_match('/[A-Z0-9]{8}([-_][A-Z0-9]{8}){3}/', $line, $match);
if ($match) {
$codes[$match[0]] = $match[0];
}
}
fclose($codeFile);
}
return $codes;
}
You might want to set your own error handler. I've switched from file_get_contents &c to the wrapped C file I/O functions to reduce memory usage, but if the files are small enough, you don't need to make this change.
Note that you could write this as a very simple shell script. If the file lines only contain the codes, all you'd need is
Code:
#!/bin/bash
sort $1 | uniq > $2