php - How To Get Data From Array Into Variable -
i'm trying data array created using file_get_contents. file adif (*.adi) output of ham radio logging software logs contacts made on ham radio.
this file.
http://www.wildragon.com/stackoverflow.adi
what need extract value of <"call:6"> in case zl3dac , value need work with. "call" callsign of station worked , "6" number of characters in value.
here's code i'm using.
$filetoread = // defined earlier in script. function goback() // defined earlier in script. if (file_get_contents($filetoread)) { echo "<br/>\nsuccess."; } else { echo "<br/>\nfailed read adi."; goback(); die(); } $array = explode("<eor>", $x); sscanf ($x, "<call:%d>%s ",$length,$rawcall); $callsign = substr($rawcall,0,$length); print_r($callsign);
i'm getting "success" file_get_contents function, i'm not getting result print_r($callsign);
what doing wrong please, , need make work please?
the reason why doesn't work because sscanf()
attempts match beginning of string. in order parse string function must first use simple string manipulation.
get contents of file parse.
$contents = file_get_contents($file);
first search position of string 'call:' using strpos()
. take sub-string starting found position , forward using substr()
.
$temporary = substr($contents, strpos($contents, 'call:'));
with example data provided variable temporary has value of: call:6>zl3dac <cont:2>oc <country:11>new zealand...
until end of data string.
we can calculate position of next opening tag. assumes opening tag entry different data , should therefore past data want.
now can extract sub-string once more. string should contain data want.
$string = substr($temporary, 0, strpos($temporary, '<'));
since sscanf()
returns array amount of placeholder defined in same order if no optional parameters provided. sscanf()
requires optional parameters parsed references have used list()
saves using references (references not use variable optimization).
list($length, $callsign) = sscanf($string, "call:%d>%s");
using sample data provided variable $callsign
contains value of zl3dac
, variable $length
value of 6
.
alternative
you use regular expression. regexes slow compared other tasks, because have work beforehand think regex acceptable here. code such solution be.
$matches = []; if(preg_match('~<call:(\d+)>(\w+)~', $contents, $matches) == false) { throw new \runtimeexception('could not match callsign input string.'); }
since preg_match
populates first element in $matches
matched string raw source. can extract values variables above.
with raw string:
list($raw, $length, $callsign) = $matches;
without raw string:
list($length, $callsign) = array_slice($matches, 1);
fetching multiple values regular expression
you can utilize regular expression function preg_match_all()
same regex above. need add flag preg_set_order
easier extract result sets. using this sample data example be:
$matches = []; preg_match_all('~<call:(\d+)>(\w+)~', $str, $matches, preg_set_order);
this result in $matches
containing array of:
array (size=3) 0 => array (size=3) 0 => string '<call:10>zl100anzac' (length=19) 1 => string '10' (length=2) 2 => string 'zl100anzac' (length=10) 1 => array (size=3) 0 => string '<call:5>h44ms' (length=13) 1 => string '5' (length=1) 2 => string 'h44ms' (length=5) 2 => array (size=3) 0 => string '<call:6>sv9cvy' (length=14) 1 => string '6' (length=1) 2 => string 'sv9cvy' (length=6)
where first element raw matched string. second length of callsign , third callsign itself.
happy coding!
Comments
Post a Comment