【问题标题】:How can I make sure the code runs all possibilities?如何确保代码运行所有可能性?
【发布时间】:2016-01-16 11:40:12
【问题描述】:

我被困住了!

代码用 preg_match_all 查找单词或句子,然后替换单词并转换单位。

长文本通过explode("\n", $Long_Text);变成$Lines[]

for 循环贯穿 $Lines。

If(preg_match_all) 查找该行是否包含它正在查找的内容。

出现的问题:

两个相同的preg_match_all() 是正确的,但是因为代码在每一行上只查找一次,所以它会跳过它。

其他问题:

3500 英尺被视为 500 英尺,因为 preg_match_all("/(\d{3,4})(ft)/", $Lines[$i], $output_array) 有 3 位数字选项。 如何强制它先查找 4 位数字,然后再查找 3 位数字。 我唯一的选择是两个 preg_matches,第一个是 {4},下一个是 {3}

Long_Text 示例:

伊斯比尔丁 Område 1a,1b,1c,2a,2b,3c,4a:在 hela perioden lätt 下直到 måttlig isbildning mellan 1500 英尺 och FL090。 Område 3a,3b: I början av perioden lätt until måttlig isbildning mellan FL060 och FL090.I mitten och mot slutet av perioden lätt till måttlig isbildning mellan 1500 英尺 och FL090。 Område 4b: Mot slutet av perioden lätt till måttlig isbildning mellan FL060 和 FL090。

Sikt/Väder/Moln Område 2a:在 hela perioden sikt över 8km 下,lokalt 3000-5000m i 斯诺福尔。 Molnbas över 2000 英尺,lokalt 1500-2000 英尺。莫尔诺维西达 FL125。 Område 1b: I början av perioden sikt över 8km, lokalt under 1500m i 迪斯。 Molnbas över 2000 英尺,lokalt 500-1000 英尺。 Molnöversida >FL125。我 mitten och mot slutet av perioden sikt över 8km, lokalt 1500-3000m i 斯诺福尔。 Molnbas över 2000 英尺,lokalt 1000-1500 英尺。莫尔诺维西达 FL125。 Område 4b,4c:在 hela perioden sikt över 下 8 公里。 Molnbas över 2000 英尺。 Molnöversida:Ingen 预测。 Område 3a,3b:在 hela perioden sikt över 下 8 公里。 Molnbas över 2000 英尺。 Molnöversida >FL125。 Område 1c,2b,3c,4a: I början och mitten av perioden sikt över 8km, lokalt 低于 1500m i dis。 Molnbas över 2000 英尺,lokalt 500-1000 英尺。 Molnöversida >FL125。 Mot slutet av perioden sikt över 8km, lokalt 3000-5000m 我 snöfall。 Molnbas över 2000 英尺,lokalt 1500-2000 英尺。 Molnöversida >FL125。 Område 1a:在 hela perioden sikt över 8km 下,lokalt 1500-3000m i 斯诺福尔。 Molnbas över 2000 英尺,lokalt 1000-1500 英尺。莫尔诺维西达 FL125。

for($i=0;$i<count($Lines);$i++) {
  if(preg_match_all("/(MELLAN) (\d{2}) (OCH) (\d{2}) (UTC)/", $Lines[$i], $output_array)){
    $Start = $output_array[1][0] . " " . $output_array[2][0] . " " . $output_array[3][0] . " " . $output_array[4][0] . " " . $output_array[5][0];
    $Goal =  $output_array[1][0] . " " . 1*($output_array[2][0]+$Diff) . " " . $output_array[3][0] . " " . 1*($output_array[4][0]+$Diff)  . " SVENSK TID";
    $Lines[$i] = str_replace($Start, $Goal, $Lines[$i]);
  }

  if(preg_match_all("/(\d{3,4}) (fot)/", $Lines[$i], $output_array)){
    $Start = $output_array[1][0] . " " . $output_array[2][0];
    $output_array[1][0] = ConvertMeter($output_array[1][0]);
    $Goal =  $output_array[1][0] . " meter";
    $Lines[$i] = str_replace($Start, $Goal, $Lines[$i]);
  }

  if(preg_match_all("/(\d{3,4}) (ft)/", $Lines[$i], $output_array)){
    $Start = $output_array[1][0] . " " . $output_array[2][0];
    $output_array[1][0] = ConvertMeter($output_array[1][0]);
    $Goal =  $output_array[1][0] . " meter";
    $Lines[$i] = str_replace($Start, $Goal, $Lines[$i]);
  }

  if(preg_match_all("/(\d{3,4})-(\d{3,4})(ft)/", $Lines[$i], $output_array)){
    $Start = $output_array[1][0] ."-" . $output_array[2][0] . "ft";
    $output_array[1][0] = ConvertMeter($output_array[1][0]);
    $output_array[2][0] = ConvertMeter($output_array[2][0]);
    $Goal =  $output_array[1][0] . "-" . $output_array[2][0] . " meter";
    $Lines[$i] = str_replace($Start, $Goal, $Lines[$i]);
  }

  if(preg_match_all("/(\d{3,4})(ft)/", $Lines[$i], $output_array)){
    $Start = $output_array[1][0] . $output_array[2][0];
    $output_array[1][0] = ConvertMeter($output_array[1][0]);
    $Goal =  $output_array[1][0] . " meter";
    $Lines[$i] = str_replace($Start, $Goal, $Lines[$i]);
  } 

  if(preg_match_all("/(FL)(\d{2,3})/", $Lines[$i], $output_array)){
    $Start = $output_array[1][0] . $output_array[2][0];
    $output_array[2][0] = ConvertMeter($output_array[2][0]*100);
    $Goal =  $output_array[2][0] . " meter";
    $Lines[$i] = str_replace($Start, $Goal, $Lines[$i]);
  }

  if(strpos($Lines[$i], "&gt;") !== false) $Lines[$i] = str_replace("&gt;", "över ", $Lines[$i]);
}

我真的很讨厌 stackoverflow 上的格式。

【问题讨论】:

  • 第二个问题:\b(?&lt;range&gt;(\d+)-(\d+)(?:ft|m|km)),见demo here on regex101
  • 谢谢!效果很好。

标签: php regex for-loop


【解决方案1】:

我可以提出有史以来最丑陋的解决方案....

preg_match_all("/\b(\d+)(ft)/", $Lines[$i], $output_array);
                    $x = count($output_array[1]);
                    for($d=0;$d<$x;$d++){
                        if(preg_match_all("/\b(\d+)(ft)/", $Lines[$i], $output_array)){
                            $Start = $output_array[1][0] . $output_array[2][0];
                            $output_array[1][0] = ConvertMeter($output_array[1][0]);
                            $Goal =  $output_array[1][0] . " meter";
                            $Lines[$i] = preg_replace("/".$Start."/", $Goal, $Lines[$i],1);
                        } 
                    }

首先我 preg_match 它以查看“值”是否存在。 我对数组进行计数,并根据 preg_match 找到值的次数启动 forloop。

它可以完成这项工作,但它很丑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2022-01-22
    • 2011-01-07
    • 2014-08-12
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    相关资源
    最近更新 更多