2012-02-18 4 views
0

PHP 정규식을 사용하여 다음 샘플에서 날짜 문자열의 위치를 ​​얻는 방법은 무엇입니까?PHP에서 정규식을 사용하여 날짜 문자열 위치를 얻는 방법

예를 들어, "2011 년 12 월 31 일"또는 "2011 년 12 월 31 일"의 위치를 ​​알고 싶습니다. 날짜 형식은 "년 월 일"또는 "월 일 년"입니다

<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> 

답변

0
<?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 
?> 
+0

감사합니다! 나는 아직도 w + mean을 알고 싶다. \ d {1,2}는 무엇을 의미합니까? d {4}는 무엇을 의미합니까? –

+0

그건 당신이 언급 한 것과 같은 날짜 문자열과 일치하는 '정규식'패턴입니다. 정규 표현식은 설명문으로 설명하기에는 너무 복잡하지만 http://php.net/manual/en/book.pcre.php에서 자세한 내용을 배울 수 있습니다. –

0

당신은 반드시 정규 표현식을 사용할 필요가 없습니다. PHP 기본 제공 함수 strpos (http://us2.php.net/manual/en/function.strpos.php)는 전달한 문자열의 첫 번째 문자열 색인을 제공합니다.

그래서 당신은 달의 이름에 대한 문자열을 검색 할 수 있습니다, 즉 뭔가 같은 : 답장을

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

날짜 패턴과 일치해야하지만 정확한 날짜는 일치하지 않아야합니다. –

관련 문제