forestpest Posted October 19, 2008 Share Posted October 19, 2008 Hi all I need to check a variable is in the correct format say it called $message the correct format must be as follows 15 digits then 8 for date like yyyymmdd then fs,x,y,x,y,x,y,ff,vs x,y,x,y,x,y,vf where fs = start point for first set of data that consists of x,y = float numbers likes 1.0,2.0 and ends with ff then vs = start point for second set of data that consists of x,y = float numbers likes 1.0,2.0 and ends with vf then 15 zeros that means we have reached the end of the data Can you help me out on this? is RE the right why to go since i don't know the length of the message i will be receiving? Quote Link to comment Share on other sites More sharing options...
forestpest Posted October 19, 2008 Author Share Posted October 19, 2008 Sorry for replying my own message just missing a comma in the format. Correct format is fs,x,y,x,y,x,y,ff,vs,x,y,x,y,x,y,vf Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 19, 2008 Share Posted October 19, 2008 Could you post a sample data set as well, not just the letters. Quote Link to comment Share on other sites More sharing options...
forestpest Posted October 20, 2008 Author Share Posted October 20, 2008 Hi all things have changed just a bit but still I can't get around this the correct format must be as follows 15 digits then 8 for date like yyyymmdd then fs,y,y,y,ff where fs = start point for first set of data that consists of y = float number like +1.0,-2.0 and ends with ff then 15 zeros that means we have reached the end of the data Can you help me out on this? is RE the right why to go since i don't know the length of the message i will be receiving? Sample Data ========== message=55901392696301120081019194525fs-0.1,+0.2,-4.3,+5.1,ff,000000000000000 15 digits = 559013926963011 date = 20081019 (yesterday) time = 194525 (19:45:25) then fs and the data = -0.1,+0.2,-4.3,+5.1 where number delimeter is . and data delimeter is ,them ff then 15 zeros. Quote Link to comment Share on other sites More sharing options...
ghostdog74 Posted October 20, 2008 Share Posted October 20, 2008 if you are not familiar with regex, then use PHP's string functions and break your data down into manageable chunks. for eg, if you want to check first 15 chars whether they are numbers if ( is_numeric( substr($message,0,15) ) ) { echo "yes, numbers\n"; } if you want to check date, you can use the checkdate() function $year = substr($message,15,4); $month = substr($message,19,2); $day = substr($message,21,2); if ( checkdate($month,$day,$year) ){ echo "Date ok\n"; } I leave it to you to do the rest. check the PHP manual for string function references. explode, implode can also be use to split,join your comma separated characters towards the end of the string. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 20, 2008 Share Posted October 20, 2008 Probably really messy regex but eh, it does the job as long as the data is consistent. $string = "message=55901392696301120081019194525fs-0.1,+0.2,-4.3,+5.1,ff,000000000000000"; preg_match("#message=(\d{15})(\d{4})(\d{2})(\d{2})(\d{6})fs(.*?)ff,([0]{15})#s", $string, $matches); $data = array_splice($matches,1); //remove first element, we don't need it list($digits,$year,$month,$day,$time,$fs,$zeros) = $data; echo "<pre>", print_r($data), "</pre>"; Quote Link to comment Share on other sites More sharing options...
forestpest Posted October 20, 2008 Author Share Posted October 20, 2008 looks good thanks I had something similar at first but I messed up. one question though how am i sure that after the fs I have only floats and not anything else? am I missing something? 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.