Jump to content

Understanding Regular Expressions


php2010

Recommended Posts

I've been reading as much as I can but I can't seem to figure this out. I'm confused by arrays, exploding, and organizing the data.

 

&&

LAT...LON 4151 7448 4160 7438 4164 7427 4161 7424
      4159 7414 4162 7413 4162 7410 4159 7405
      4159 7396 4145 7399 4149 7393 4150 7385
      4121 7385 4103 7450 4108 7451 4119 7438
      4135 7471 4140 7475 4150 7475
TIME...MOT...LOC 0838Z 236DEG 47KT 4159 7465 4137 7463
          4098 7497

$$

 

The data I would like to get is between: 'LAT...LON' and 'TIME...MOT'. Is the best way of doing this like below?:

 

preg_match_all('/LAT...LON (.*)\nTIME...MOT/Uism',$data,$results); $SVR = $results[1];

 

The problem I'm having, I want to explode all of those numbers and convert them over to LAT/LON (xx.xx, -yy.yy) and echo the first lat/lon at the end to complete the "line". While also keeping EACH section separate to it's own line and echo them out like this:

 

Color: 000 000 255
Line: 6,0,"blah"
41.51, -74.48
41.60, -74.38
41.64, -74.27
41.61, -74.24
41.59, -74.14
41.62, -74.13
41.62, -74.10
41.59, -74.05
41.59, -73.96
41.45, -73.99
41.49, -73.93
41.51, -74.48
End:

Color: 000 000 255
Line: 6,0,"blah"
SECOND SET OF LAT/LON HERE
End:

Color: 000 000 255
Line: 6,0,"blah"
THIRD SET OF LAT/LON HERE
End:

 

So on and so forth. After I figure out how to keep everything separate I should be able to figure it out. I'm having some real trouble with the arrays. :shy:

 

Here's the current file I've figured out so far:

 

		$file = fopen('svr.txt','r');
	while($t = fread($file,102465)){ $data .= $t; }
	fclose($file);

	preg_match_all('/LAT...LON (.*)\nTIME...MOT/Uism',$data,$results); $SVR = $results[1];
	$SVR = preg_replace('/\n/', ' ', $SVR);
	$SVR = preg_replace('/\s\s+/', ' ', $SVR); 
	unset($data);

	header("Content-Type: text/plain");

	$svrlatlon = array_unique($SVR);
	$svrwarning1 = $svrlatlon[0];
	$svrwarning1 = explode(" ", $svrwarning1);
	$svrwarning1count = count($svrwarning1);
	$svrwarning2 = $svrlatlon[1];
	$svrwarning2 = explode(" ", $svrwarning2);
	$svrwarning2count = count($svrwarning2);
	$svrwarning3 = $svrlatlon[2];
	$svrwarning3 = explode(" ", $svrwarning3);
	$svrwarning3count = count($svrwarning3);
	$svrwarning4 = $svrlatlon[3];
	$svrwarning4 = explode(" ", $svrwarning4);
	$svrwarning4count = count($svrwarning4);

	echo "Severe Thunderstorm 1 Lat Lon:\n";
	for ($i = 0; $i < $svrwarning1count; $i+= 2) {

		if ($svrwarning1[$i+1] != null) {
			echo substr($svrwarning1[$i], -4, 2) . "." . substr($svrwarning1[$i], -2, 2) . ", -" . substr($svrwarning1[$i+1], -4, 2) . "." . substr($svrwarning1[$i+1], -2, 2) . "\n";
		}
	}
	if ($svrwarning1[0] != null){
	echo substr($svrwarning1[0], -4, 2) . "." . substr($svrwarning1[0], -2, 2) . ", -" . substr($svrwarning1[1], -4, 2) . "." . substr($svrwarning1[1], -2, 2) . "\n\n\n";
	}

	echo "Severe Thunderstorm 2 Lat Lon:\n";
	for ($i = 0; $i < $svrwarning2count; $i+= 2) {

		if ($svrwarning2[$i+1] != null) {
			echo substr($svrwarning2[$i], -4, 2) . "." . substr($svrwarning2[$i], -2, 2) . ", -" . substr($svrwarning2[$i+1], -4, 2) . "." . substr($svrwarning2[$i+1], -2, 2) . "\n";
		}
	}
	if ($svrwarning2[0] != null){
	echo substr($svrwarning2[0], -4, 2) . "." . substr($svrwarning2[0], -2, 2) . ", -" . substr($svrwarning2[1], -4, 2) . "." . substr($svrwarning2[1], -2, 2) . "\n\n\n";
	}

	echo "Severe Thunderstorm 3 Lat Lon:\n";
	for ($i = 0; $i < $svrwarning3count; $i+= 2) {

		if ($svrwarning3[$i+1] != null) {
			echo substr($svrwarning3[$i], -4, 2) . "." . substr($svrwarning3[$i], -2, 2) . ", -" . substr($svrwarning3[$i+1], -4, 2) . "." . substr($svrwarning3[$i+1], -2, 2) . "\n";
		}
	}
	if ($svrwarning3[0] != null){
	echo substr($svrwarning3[0], -4, 2) . "." . substr($svrwarning3[0], -2, 2) . ", -" . substr($svrwarning3[1], -4, 2) . "." . substr($svrwarning3[1], -2, 2) . "\n\n\n";
	}

	echo "Severe Thunderstorm 4 Lat Lon:\n";
	for ($i = 0; $i < $svrwarning4count; $i+= 2) {

		if ($svrwarning4[$i+1] != null) {
			echo substr($svrwarning4[$i], -4, 2) . "." . substr($svrwarning4[$i], -2, 2) . ", -" . substr($svrwarning4[$i+1], -4, 2) . "." . substr($svrwarning4[$i+1], -2, 2) . "\n";
		}
	}
	if ($svrwarning4[0] != null){
	echo substr($svrwarning4[0], -4, 2) . "." . substr($svrwarning4[0], -2, 2) . ", -" . substr($svrwarning4[1], -4, 2) . "." . substr($svrwarning4[1], -2, 2) . "\n\n\n";
	}

 

If anyone knows how to do this, can you explain it or add comment sections to the code so I can better understand how to achieve this? Thanks.  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.