<?
function find_apn_by_code($filename, $plmn) {
    if (!file_exists($filename)) {
        return 0;
    }

    $handle = fopen($filename, "r");
    if (!$handle) {
        return 0;
    }

    // Read the header line
    $headers = 0;
    $plmn_index=-1;
    while (($line = fgets($handle))) {
        //$line = trim($line);//, "\xEF\xBB\xBF\n\r"); // Remove BOM and newlines

        if (!$headers) {
            $headers = explode($line,';');
            //print_r($headers);
            for ($i=0;$i<sizeof($headers);$i++) {
                if ($headers[$i]=='PLMN') {
                    $plmn_index=$i;
                    break;
                }
            }
            if ($plmn_index==-1) return;
            continue;
        }

        $fields = explode( $line,';');
        if (count($fields) < count($headers)) {
            continue; // skip malformed lines
        }

        //$record = array_combine($headers, $fields);

        if ($fields[$plmn_index] == $plmn) {
            fclose($handle);
            $record=array();
            for ($i=0;$i<sizeof($headers);$i++) {
                $record[$headers[$i]]=$fields[$i];
            }
            return $record;
        }
        if (feof($handle)) break;
    }

    fclose($handle);
    return 0; // PLMN not found
}


?>