views:

119

answers:

1

Hi there

I'm parsing some data using DOMDocument after fetching HTML file using curl. The codes look like this


$dom = new DOMDocument();
@$dom->loadHTML($content);
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item($tblNum)->getElementsByTagName('tr');

foreach ($rows as $row) {
    $cols = $row->getElementsByTagName('td');
    $var = $cols->item(1)->nodeValue; // The problem is here
}

I can't convert $var to a timestamp using strtotime. I don't know why, I know $cols->item(1)->nodeValue returned the value I want, I even tried exploing and imploding it into another variable, but I still can't convert it to a timestamp using strtotime. I've also tested the value directly

strtotime('11 Jan 2010');

and it did return a timestamp, so, what should I do ?

A: 

I can't reproduce the problem using php 5.3.1/winxp

$content = '<html><head><title>...</title></head><body>
  <table>
    <tr><td>X</td><td>12 Jan 2010</td></tr>
  </table>
</body></html>';
$tblNum = 0;

$dom = new DOMDocument();
@$dom->loadHTML($content);
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item($tblNum)->getElementsByTagName('tr');

foreach ($rows as $row) {
  $cols = $row->getElementsByTagName('td');
  $var = $cols->item(1)->nodeValue;
  echo date('Y-m-d H:i:s', strtotime($var)), "\n";
}

prints 2010-01-12 00:00:00

VolkerK
I'm using PHP 5.2.10 on Linux, I'll test the code in Windows later
aurorius
I get the same result with PHP 5.2.12-pl0-gentoo
VolkerK
Apprently, there's a   before the value, you can try reproducing the problem by adding   in front of the date, so, in the end, I just use $content=str_replace(' ', '', $content); coz $var=str_replace(' ', '', $var) doesn't work. Thanks !
aurorius