Jump to content

Specific regualar expressions I need but unsure of how to express


forestpest

Recommended Posts

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?

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.

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.

 

 

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>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.