A question which I am occasionally asked is “how to read ADIF into a database?”. For me this is really about reading a file into PHP, to break up the contents of the ADIF line by line.
One of the problems that ADIF poses (prior to ADIF 3.0) is that the file format is not especially friendly for regular expressions. Each field is of this form:
<FIELD:length>value
The length refers to the number of characters in the value string, which is an unusual structure that means each element in the ADIF is a little more involved than, say, hunting for sub-strings.
A good function for this kind of problem is PHP’s sscanf(), which is designed for scanning within sub-strings and extracting data:
mixed sscanf ( string $str , string $format [, mixed &$… ] )
The function sscanf() is the input analog of printf(). sscanf() reads from the string str and interprets it according to the specified format, which is described in the documentation for sprintf().Any whitespace in the format string matches any whitespace in the input string. This means that even a tab \t in the format string can match a single space character in the input string.
So, let’s assume you have read in the contents of an ADIF file and used something like explode() to turn it into an array of entries ending EOR. Let’s assume you have a line of the ADIF in a variable, $x.
Let’s use sscanf() to get the callsign out of the ADIF:
sscanf ($x, "<CALL:%d>%s ",$length,$rawcall);
This means $rawcall is set to equal the string after the > symbol, and $length is the number after the colon. By using the delimiter %d we ensure that this value is numeric. The value of $rawcall can be pretty much anything as %s allows any string.
Next, to be rigorous we should ensure we only read $length characters of $rawcall. This is important to protect you against faulty ADIF files. It also helps you avoid reading in whitespace in some cases.
$callsign = substr($rawcall,0,$length);
Now we have $callsign, and you can repeat the same tests for other fields in the ADIF. Don’t forget to escape all user inputs – for example, using mysqli_real_escape_string, before you put them into an SQL insert.
No, it’s not as easy as we’d like – but don’t forget, ADIF 3.x now supports XML (and things are going to get a great deal easier for programmers!).
Great post Mike, I get asked this alot too.. I found a nice PHP class written by kj4iwx which makes dealing with ADIF files very easy which can be found at https://bitbucket.org/kj4iwx/phpadifparser/
Will Clublog be supporting ADIF 3? as I’m thinking of building it into Cloudlog in the next couple of weeks based on the current spec.
Pete, 2E0SQL
Without a doubt, yes – Club Log will support ADIF 3 and XML.