我正在开发一个PHP脚本,它将读取METAR天气预报,并告知我是否下雨。可能的降水报告为:
DZ, GR, GS, IC, PL, RA, SG, SN, and UP
问题是有时它们是组合在一起的,前面也可能有+或 - 所以这些可能发生:
+RA
-RA
RASN
DZIC
etc.
有没有办法在两个字母变量的右侧和左侧设置通配符以适应这些可能性?
$metar = "2013/12/30 00:51 KALB 300051Z 33006KT 1 1/2SM -SN BR BKN004 OVC022 01/M01 A2961 RMK AO2 SFC VIS 2 RAE2354 SLP030 P0010 T00061006 $";
if ($metar == *DZ* || $metar == *GR* || $metar == *GS* || $metar == *IC* || $metar == *PL* || $metar == *RA* || $metar == *SG* || $metar == *SN* || $metar == *UP*) {
$rain == "Rain";
} else $rain == "No Rain";
答案 0 :(得分:2)
此正则表达式匹配将查找字母对的所有组合,并带有可选的前一个+
或-
字符。
// The string of weather codes
$theString = '+RA -RA RASN DZIC';
// This regex matches all combination of letter pairs with an
// optional preceding + or -
$regex = '/(?<=^|\s)[+-]?(?:DZ|GR|GS|IC|PL|RA|SG|SN|UP)+(?=$|\s)/';
// Find as many matches as there are in the string
preg_match_all($regex, $theString, $matches);
$weatherCodes = array_shift($matches);
// Output the weather codes
echo '<pre>';
var_dump($weatherCodes);
echo '</pre>';
它将输出如下内容:
array (size=4)
0 => string '+RA' (length=3)
1 => string '-RA' (length=3)
2 => string 'RASN' (length=4)
3 => string 'DZIC' (length=4)
如果您想要更有用的数据格式的天气代码组合,那么您可以使用这样的函数来解析它们。
function getWeatherCodes($codes) {
$regex = '/(?<=^|\s)[+-]?(?:DZ|GR|GS|IC|PL|RA|SG|SN|UP)+(?=$|\s)/';
preg_match_all($regex, $codes, $matches);
$matches = array_shift($matches);
foreach ($matches as & $match) {
$newItem = array('sign'=>null, 'codes'=>array());
if (strlen($match) % 2 != 0) {
$newItem['sign'] = $match[0];
$match = substr($match, 1);
}
$newItem['codes'] = str_split($match, 2);
$match = $newItem;
}
return $matches;
}
$theString = 'RA +DZIC';
$weatherCodes = getWeatherCodes($theString);
echo '<pre>';
var_dump($weatherCodes);
echo '</pre>';
将输出如下内容:
array (size=2)
0 =>
array (size=2)
'sign' => null
'codes' =>
array (size=1)
0 => string 'RA' (length=2)
1 =>
array (size=2)
'sign' => string '+' (length=1)
'codes' =>
array (size=2)
0 => string 'DZ' (length=2)
1 => string 'IC' (length=2)
答案 1 :(得分:0)
这应该有效。与其他答案不同,它使用preg_match_all
来匹配$metar
字符串中的所有项目。因此RA
和SN
也匹配。我已将所有代码放入数组中,以便更轻松地添加/删除项目:
// Test data.
// $metar = "+RA -RA RASN DZIC";
$metar = "-RASN";
// Codes placed in an array.
$codes = array('DZ','GR','GS','IC','PL','RA','SG','SN','UP');
// Using a 'preg_match_all' as well as 'implode' to compare all items in the array to the test data.
preg_match_all('/' . implode('|', $codes) . '/is', $metar, $matches);
// Check the count of '$matches[0]' to see if any matches are made or not.
if (count($matches[0]) > 0) {
$rain = "Rain";
}
else {
$rain = "No Rain";
}
// Echo the results.
echo $rain;
您可以使用此方法进行调试,以查看$matches
的输出:
// Dump the '$matches' for debugging.
echo '<pre>';
print_r($matches);
echo '</pre>';
使用我的例子的输出是:
Array
(
[0] => Array
(
[0] => RA
[1] => SN
)
)
答案 2 :(得分:-1)
$metar = "-RASN";
if (preg_match('/DZ|GR|GS|IC|PL|RA|SG|SN|UP/', $metar)) {
$rain = "Rain";
} else {
$rain = "No Rain";
}
如果始终以“+”或“ - ”开头,则可以执行以下操作:
if (preg_match('/^(\+|-)?(DZ|GR|GS|IC|PL|RA|SG|SN|UP)/', $metar)) {
$rain = "Rain";
} else {
$rain = "No Rain";
}
有关详细信息,请参阅http://php.net/preg_match。
如果您需要打印匹配项,您应该执行以下操作:
if (preg_match_all('/(\+|-)?(DZ|GR|GS|IC|PL|RA|SG|SN|UP)/', $metar, $matches)) {
echo 'Rain (matching codes: ' . implode(', ', $matches[0]) . ')';
} else {
echo "No Rain";
}
答案 3 :(得分:-1)
使用此:
<?php
$metar = "-RASN";
if (preg_match("/[+-]?(DZ|GR|GS|IC|PL|RA|SG|SN|UP)/", $metar)) {
$rain = "Rain";
}
else {
$rain = "No Rain";
}
?>
请注意,它必须是$rain = "Rain"
,即一个 =
,这意味着分配。 ==
用于比较。