我需要帮助编写一个脚本,该脚本在字符串中搜索可能出现的日期。
它应该查找所有形式的日期示例:
Dd-m1yyyy
- 任何一天的提及
sat-sun
- 任何具体的确切日期
Sunday 28th July
ETC...
然后返回找到的日期
您可以使用 preg_match 或 preg_match_all 函数
http://php.net/manual/en/function.preg-match.php
http://www.php.net/manual/en/function.preg-match-all.php
这是正则表达式语法的指南
http://www.php.net/manual/en/regexp.reference.meta.php
您将需要创建一个您尝试提取的所有可能日期格式的列表,然后构建与这些日期匹配的正则表达式。
如果您可以发布所有这些日期格式的列表,那么我相信如果您遇到困难,人们会帮助您创建正则表达式
示例日期 01-12-1970
$pattern = "/\d{2}\-\d{2}\-\d{4}/";
if (preg_match($pattern, $content, $matches)) {
echo "Found Match on {$matches[0]}";
}
示例日期:7 月 28 日星期日
这个会匹配
跟随任何单词(假设它是一个月,如果模式的其余部分匹配)
$pattern = "/\w+day\s\d{1,2}(st|th|rd)\s\w+/";
if (preg_match($pattern, $content, $matches)) {
echo "Found Match on {$matches[0]}";
}
如果您需要测试您的正则表达式,有很多在线正则表达式工具。
我应该建议Preg_Match
(http://php.net/manual/en/function.preg-match.php)
此类日期的示例:04-09-1990
$matches = array();
preg_match('/(\d{2})-(\D*)-(\d{4})/', $string, $matches)
扩展您想要的正则表达式。很好的解释http://www.regular-expressions.info/dates.html
*提示在http://regexlib.com/上搜索合适的正则表达式
正则表达式将是最佳解决方案,但您也可以使用由您正在查找的内容组成的数组。然后使用 strpos 遍历数组以查找您要查找的字符串是否(以及在哪里)存在。关注星期几和月份的名称和缩写。如果您搜索一个月中的某一天,您将得到太多的误报结果。