简体   繁体   English

如何在PHP中使用正则表达式获取日期字符串位置

[英]How to get date string position using regular expression in PHP

How to use php regular expression to get the position of date string in the following sample? 在以下示例中,如何使用php正则表达式获取日期字符串的位置?

For example, I want know the position of "December 31, 2011" or "December 31 2011". 例如,我想知道“ 2011年12月31日”或“ 2011年12月31日”的职位。 Date format is "month day, year" or "month day year" 日期格式为“年月日”或“年月日”

<TR>
<TD VALIGN="top" ALIGN="center"><FONT STYLE="font-family:Times New Roman" SIZE="2"><B>December 31, 2011</B></FONT></TD></TR>
</TABLE> <P STYLE="margin-top:0px;margin-bottom:0px" ALIGN="center"><FONT STYLE="font-family:Times New Roman" SIZE="1"><B>(Date of Event Which Requires Filing of this Statement) </B></FONT></P>
<P STYLE="font-size:12px;margin-top:0px;margin-bottom:0px">&nbsp;</P><center> <P STYLE="line-height:6px;margin-top:0px;margin-bottom:2px;border-bottom:1pt solid #000000;width:21%">&nbsp;</P></center> <P STYLE="margin-top:12px;margin-bottom:0px"><FONT
STYLE="font-family:Times New Roman" SIZE="2"><B>Check the appropriate box to designate the rule pursuant to which this Schedule is filed: </B></FONT></P> <P STYLE="margin-top:12px;margin-bottom:0px; margin-left:8%"><FONT
STYLE="font-family:Times New Roman" SIZE="2"><B></B><FONT STYLE="FONT-FAMILY:WINGDINGS">&#120;</FONT><B></B><B> Rule 13d-1 (b) </B></FONT></P> <P STYLE="margin-top:12px;margin-bottom:0px; margin-left:8%"><FONT
STYLE="font-family:Times New Roman" SIZE="2"><B></B><FONT STYLE="FONT-FAMILY:WINGDINGS">&#168;</FONT><B></B><B> Rule 13d-1 (c) </B></FONT></P> <P STYLE="margin-top:12px;margin-bottom:0px; margin-left:8%"><FONT
STYLE="font-family:Times New Roman" SIZE="2"><B></B><FONT STYLE="FONT-FAMILY:WINGDINGS">&#168;</FONT><B></B><B> Rule 13d-1 (d) </B></FONT></P> <P STYLE="font-size:12px;margin-top:0px;margin-bottom:0px">&nbsp;</P>
<TABLE STYLE="BORDER-COLLAPSE:COLLAPSE" BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<?php
$str = '<TR>
<TD VALIGN="top" ALIGN="center"><FONT STYLE="font-family:Times New Roman" SIZE="2"><B>December 31, 2011</B></FONT></TD></TR>
</TABLE> <P STYLE="margin-top:0px;margin-bottom:0px" ALIGN="center"><FONT STYLE="font-family:Times New Roman" SIZE="1"><B>(Date of Event Which Requires Filing of this Statement) </B></FONT></P>
<P STYLE="font-size:12px;margin-top:0px;margin-bottom:0px">&nbsp;</P><center> <P STYLE="line-height:6px;margin-top:0px;margin-bottom:2px;border-bottom:1pt solid #000000;width:21%">&nbsp;</P></center> <P STYLE="margin-top:12px;margin-bottom:0px"><FONT
STYLE="font-family:Times New Roman" SIZE="2"><B>Check the appropriate box to designate the rule pursuant to which this Schedule is filed: </B></FONT></P> <P STYLE="margin-top:12px;margin-bottom:0px; margin-left:8%"><FONT
STYLE="font-family:Times New Roman" SIZE="2"><B></B><FONT STYLE="FONT-FAMILY:WINGDINGS">&#120;</FONT><B></B><B> Rule 13d-1 (b) </B></FONT></P> <P STYLE="margin-top:12px;margin-bottom:0px; margin-left:8%"><FONT
STYLE="font-family:Times New Roman" SIZE="2"><B></B><FONT STYLE="FONT-FAMILY:WINGDINGS">&#168;</FONT><B></B><B> Rule 13d-1 (c) </B></FONT></P> <P STYLE="margin-top:12px;margin-bottom:0px; margin-left:8%"><FONT
STYLE="font-family:Times New Roman" SIZE="2"><B></B><FONT STYLE="FONT-FAMILY:WINGDINGS">&#168;</FONT><B></B><B> Rule 13d-1 (d) </B></FONT></P> <P STYLE="font-size:12px;margin-top:0px;margin-bottom:0px">&nbsp;</P>
<TABLE STYLE="BORDER-COLLAPSE:COLLAPSE" BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>';

$matches = array();
preg_match('/(\w+ \d{1,2},? \d{4})/', $str, $matches); // search for date

$position = strpos($str, $matches[0]);  // Find position
?>

You don't necessarily have to use regular expressions. 您不必一定要使用正则表达式。 The php built-in function strpos (http://us2.php.net/manual/en/function.strpos.php) will give you the string index of the first occurrence of whatever you pass to it. php内置函数strpos(http://us2.php.net/manual/zh/function.strpos.php)将为您提供传递给它的所有内容的第一次出现的字符串索引。

So you can search the string for the name of the month, ie something like: 因此,您可以在字符串中搜索月份的名称,例如:

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM