douhui1630 2018-04-17 03:26
浏览 116

使用PHP在括号内提取时间

This is my first time using the trim function and I want to get the Time excluding the open and close parenthesis of my string. Can you give me hints and suggestions on how to do this?

$string = "Updated by Carewina Almonte  (04/02/2018 21:58:32)";     
echo trim($string, "Updated by Carewina Almonte  (04/02/2018");     
exit();
  • 写回答

4条回答 默认 最新

  • duanpi7107 2018-04-17 03:33
    关注

    You can use preg_match for this task:

    $str = 'Updated by Carewina Almonte  (04/02/2018 21:58:32)';
    preg_match('/(?<=\(\d{2}\/\d{2}\/\d{4} ).*(?=\))/', $str, $match);
    echo $match[0];
    

    Breakdown:

    • Positive Lookbehind (?<=\(). Assert that the Regex below matches:

      • \( matches the character ( literally (case sensitive)
      • .* matches any character (except for line terminators)
      • * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      • \d{2} matches a digit (equal to [0-9])
      • {2} Quantifier — Matches exactly 2 times
      • \/ matches the character / literally (case sensitive)
    • Positive Lookahead (?=\)). Assert that the Regex below matches:

      • \) matches the character ) literally (case sensitive)
    评论

报告相同问题?

悬赏问题

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