从DOM元素获取日期

时间:2014-01-11 13:31:58

标签: php dom simple-html-dom

我正在尝试使用Simple HTML DOM从子信息中获取日期01/08/14

我的代码是:

HTML:

<div id="display_cr" class="loose-spacing tid">    
    <div class="review_by">By <strong>juand1</strong> on 01/08/14 06:51 AM (PST)</div>
    <div class="review_by">By <strong>juand1</strong> on 01/08/14 06:51 AM (PST)</div>
</div>

PHP:

protected function scrap_date($review) {
    $res  = ''; 
    $date = $review->find('div[class=review_by]');   
    if (isset($date)) { 
        foreach ($date as $div) {
            $data = '';
            foreach ($review->find('div[class=review_by]') as $element) {  
                $data = $element->outertext;
            }
            $res[] = $data;
        }  
    }
    return $res;
}

1 个答案:

答案 0 :(得分:0)

尝试

foreach ($review->find('div[class=review_by]') as $element) {
  $data = $element->innertext;
  preg_match("/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{1,2}/", $data ,$match);
  if(sizeof($match) > 0 ){
    res[] = $match[0];
  }
}