简体   繁体   中英

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?

For example, I want know the position of "December 31, 2011" or "December 31 2011". 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.

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";
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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