douhutongvm382381 2013-06-24 22:25
浏览 39
已采纳

从字符串php捕获日期短语

I have the strings (in an array):

$a = "account Tel48201389 user@whatever.net dated 2013-07-01 in JHB".

and

$b = "installation on 2013-08-11 in PE".

I need to get the full date out of each of these strings using PHP only.
is it possible to use wildcards with pregmatch?
i tried:

preg_match('/(?P<'name'>\w+): (?P'<'digit-digit-digit'>'\d+)/', $str, $matches); 

but it gives an error.
end result should be: $a = 2013-07-01" and $b = "2013-08-11" THanks!

  • 写回答

4条回答 默认 最新

  • doulupian8725 2013-06-24 22:37
    关注

    You can use preg_match_all to get all date patter in an string. All string matches will be saved in an array which should be passed as an argument to the function.

    In this example save all patterns dddd-dd-dd in the array $matches.

    $string = "account Tel48201389 user@whatever.net dated 2013-07-01 in JHB installation on 2013-08-11 in PE";
    
    if (preg_match_all("@\d{4}-\d{2}-\d{2}@", $string, $matches)) {
       print_r($matches);
    }
    

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建