cr-ispinternet Posted March 16, 2012 Share Posted March 16, 2012 Hi Guys, Think this a simple one but been stuck for a few hours and tearing my hair out... ive got a serial command querying a GPS dongle which returns an output once queried $read = $serial->readPort(); Once the query has been returned it give me and output like the following $GPRMC,120329.000,A,5310.5615,N,00226.2420,W,0.0,10.0,160312,,,A*43 $GPGGA,120329.000,5310.5615,N,00226.2420,W,1,04,25.5,-0.3,M,59.1,M,,0000*52 $GPVTG,10.0,T,,M,0.0,N,0.0,K,A*3C $PMST200,00,204*1E $PMST200,00,204*1E $PMST200,00,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $GPGLL,5310.5615,N,00226.2420,W,120329.00,A,A*72 $GPGSA,A,3,04,07,10,13,,,,,,,,,36.1,25.5,25.5*06 $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,00,204*1E $PMST200,00,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $GPRMC,120330.000,A,5310.5615,N,00226.2420,W,0.0,10.0,160312,,,A*4B $GPGGA,120330.000,5310.5615,N,00226.2420,W,1,04,25.5,-0.3,M,59.1,M,,0000*5A $GPVTG,10.0,T,,M,0.0,N,0.0,K,A*3C $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,??,204*1E $PMST200,??,204*1E Im basically trying to preg_match the GPRMC line but recover the full line, i will then explode it later and break it down by the commas i the string... $GPRMC,120329.000,A,5310.5615,N,00226.2420,W,0.0,10.0,160312,,,A*43 i need the above line each time i query the dongle rather than all the other stuff it generates any one got a clean way of doing it? ive tried this but all it give me is the following.. if (preg_match("/GPMRC/", $read, $result)) { print "$result[0]"; } which give mes this only root@voyage:/usr/utils/sattrack# ./check_gps.php GPRMC root@voyage:/usr/utils/sattrack# Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted March 16, 2012 Share Posted March 16, 2012 $str = 'GPRMC,120330.000,A,5310.5615,N,00226.2420,W,0.0,10.0,160312,,,A*4B $GPGGA,120330.000,5310.5615,N,00226.2420,W,1,04,25.5,-0.3,M,59.1,M,,0000*5A $GPVTG,10.0,T,,M,0.0,N,0.0,K,A*3C'; preg_match("~^GPRMC.*~",$str,$ms); print_r($ms); results: Array ( [0] => GPRMC,120330.000,A,5310.5615,N,00226.2420,W,0.0,10.0,160312,,,A*4B ) Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2012 Share Posted March 16, 2012 $str = 'GPRMC,120330.000,A,5310.5615,N,00226.2420,W,0.0,10.0,160312,,,A*4B $GPGGA,120330.000,5310.5615,N,00226.2420,W,1,04,25.5,-0.3,M,59.1,M,,0000*5A $GPVTG,10.0,T,,M,0.0,N,0.0,K,A*3C'; preg_match("~^GPRMC.*~",$str,$ms); print_r($ms); That solution fails with the exact content the OP posted (i.e. the leading dollar sign.) @cr-ispinternet: Is the line you want always the first line in the output? If so, a better solution might be to get the first line. $GPRMC_line = array_shift(explode("\n", $gps_output)); Although you may have to play with the delimiter, could be "\n", "\r" or "\n\r" But, if you need to use regular expression, this would work too preg_match("#$GPRMC.*#", $input, $match); $GPRMC_line = $match[0]; Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted March 16, 2012 Share Posted March 16, 2012 That solution fails with the exact content the OP posted (i.e. the leading dollar sign.) Judging from the OP's attempted regex, i assumed the dollar sign to be irrelevant. Either way you would simply add it to the beginning of the pattern. $GPRMC_line = array_shift(explode("\n", $gps_output)); This will not work, if you looked at the OP's example, the desired string appears twice. Quote Link to comment Share on other sites More sharing options...
salathe Posted March 16, 2012 Share Posted March 16, 2012 What is $read? One huge string, an array where each item in the array is line, a magical pixie elephant? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2012 Share Posted March 16, 2012 Judging from the OP's attempted regex, i assumed the dollar sign to be irrelevant. I agree, it is irrelevant with respect to the data needed, but the previous pattern would not match the lines because the pattern was looking for a line that starts with "GPRMC". Therefore, when run against the sample data provided, there were no matches. The test data showed for that first regex you provided took out the dollar sign, so it worked. $GPRMC_line = array_shift(explode("\n", $gps_output)); This will not work, if you looked at the OP's example, the desired string appears twice. I did not read every line in detail in his sample input. But, I did specifically state right before that code Is the line you want always the first line in the output? If so, a better solution might be to get the first line. And then I also gave a solution that would work across multiple lines, if needed. Quote Link to comment Share on other sites More sharing options...
cr-ispinternet Posted March 17, 2012 Author Share Posted March 17, 2012 Physco, your suggestion of the following worked a treat preg_match("#$GPRMC.*#", $input, $match); $GPRMC_line = $match[0]; gave me an ouput of the following... root@voyage:/usr/utils/sattrack# ./check_gps.php GPRMC,001025.000,V,5310.5798,N,00226.2040,W,0.0,0.0,170312,,,N*60 root@voyage:/usr/utils/sattrack# great thing now is there are a dozen other string i can latch onto using the same regex but to answer your questions the $ at the start of GPRMC string does infact mean nothing and the data returned from $read is part of the serial command issued to get the data in the first place all i need to do now is strip that result now into an array using the , as a delimiter for sectioning thanks guys ))) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.