virtuexru Posted January 21, 2009 Share Posted January 21, 2009 OK PHP Gurus, another question . I'm really struggling to get this down pact so I'm sorry if my questions are redundant. Just trying to get things to work :-P. Here is the HTML code I have: <td align="right">Sale Date :</td> <td align="LEFT" colspan="2"> 1/26/09 </td> My goal is to EXTRACT the date "1/26/09" (could be ANY date at ANY given time, so basically its always unique) into a separate variable. How should I go about doing this? I tried something like this but it didn't work: // Find date $pat = "%\A(?:^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}$)\Z%"; preg_match_all($pat, $data, $date); print_r($date); Quote Link to comment https://forums.phpfreaks.com/topic/141807-extracting-date-from-html-code/ Share on other sites More sharing options...
virtuexru Posted January 21, 2009 Author Share Posted January 21, 2009 OK, so far I have also tried this, it looks like it should work but I'm getting an output of just some blank arrays. <? // Find date $pattern = '#^(\d{1,2})[/-](\d{1,2})[/-](\d{4})$#'; preg_match_all($pattern, $data, $date); // Print print_r($date); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141807-extracting-date-from-html-code/#findComment-742453 Share on other sites More sharing options...
sasa Posted January 21, 2009 Share Posted January 21, 2009 try <?php $test = '<td align="right">Sale Date :</td> <td align="LEFT" colspan="2"> 1/02/09 01.28.1999 10*9.9/99=1 </td>'; $pat = '/[0\s]([1-9]|1[012])([- \/.])(0?[1-9]|[12][0-9]|3[01])\2((19|20)?[0-9][0-9])/'; preg_match_all($pat,$test,$out); $out = $out[0]; foreach ($out as $k => $v) $out[$k] = trim($v); print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141807-extracting-date-from-html-code/#findComment-742472 Share on other sites More sharing options...
.josh Posted January 21, 2009 Share Posted January 21, 2009 preg_match('~<td align="right">Sale Date :</td>[^<]*<td align="LEFT" colspan="2">(.*?)</td>~s',$test,$match); $date = preg_replace('~\s~','',$match[1]); Quote Link to comment https://forums.phpfreaks.com/topic/141807-extracting-date-from-html-code/#findComment-742670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.