duandai3964 2017-05-02 15:01
浏览 13
已采纳

使用具有时间间隔的数组中的正确项

I some text that looks like this:

Vind vid marken
12-14UTC: Område E3a: N/2-5 knop, byar upp till 13 knop.
Område E2b: N/2-5 knop, byar upp till 14 knop.
Område E1a,E1b: NV-N/0-6 knop, byar upp till 15 knop.
Område E2a: N/1-6 knop, byar upp till 12 knop.

14-16UTC: Område E1b: N/2-6 knop, byar upp till 16 knop.
Område E3a: N/3-6 knop.
Område E2b: N/3-5 knop.
Område E2a: N/1-5 knop, byar upp till 12 knop.
Område E1a: N/0-6 knop, byar upp till 14 knop.

16-18UTC: Område E1b: N-NO/1-7 knop, byar upp till 15 knop.
Område E2a,E2b: N/0-4 knop, byar upp till 12 knop.
Område E1a: N-NO/3-8 knop, byar upp till 15 knop.
Område E3a: N-NO/1-6 knop, byar upp till 13 knop.

18-20UTC: Område E2b,E3a: N-NO/1-5 knop.
Område E1b,E2a: N-NO/0-5 knop, byar upp till 11 knop.
Område E1a: N-NO/1-7 knop.

My goal is to find the value of the correct location and time.
As you can see the text is deceived in times and in areas (for example E3a), and it repeats the same area in the next time interval.
With this regex: preg_match_all("/(\d{2}-\d{2})UTC/", $text, $arr);

I get this array:

array(2  
      0 =>  array(4  
          0 =>  12-14UTC  
          1 =>  14-16UTC  
          2 =>  16-18UTC  
          3 =>  18-20UTC  
      )  

      1 =>  array(4  
          0 =>  12-14  
          1 =>  14-16  
          2 =>  16-18  
          3 =>  18-20  
     )  
)  

Right now the time is 14:52 UTC.
How do I... lets say echo $arr[1][1], because I know the current time is between 14 and 16?
I need the regex to be either the way it is above, or at least maintain $arr[0] because I need that in the next regex pattern.

I'm stuck here because I can't figure out how to know what time interval it is I need in the next regex.

  • 写回答

1条回答 默认 最新

  • duanlushen8940 2017-05-02 15:14
    关注

    Try

    preg_match_all("(\d{2})-(\d{2})UTC", $text, $arr);
    

    That should return:

    array(3
          0 =>  array(4
            0   =>  12-14UTC
            1   =>  14-16UTC
            2   =>  16-18UTC
            3   =>  18-20UTC
        )
      1 =>  array(4
            0   =>  12
            1   =>  14
            2   =>  16
            3   =>  18
        )
      2 =>  array(4
            0   =>  14
            1   =>  16
            2   =>  18
            3   =>  20
        )
    )
    

    From there you can use the values of array[1] and array[2] to compare against your current hour (using date("G")) either by iteration or a callback.

    Since you're going to be pulling the time values from the regex, I would also recommend you sanitize and error check your inputs as best as possible as well using perhaps intval().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度