PHP XPath DOM问题

时间:2014-07-17 13:45:36

标签: php dom xpath

我是XPath的新手,所以我确定我会犯一些菜鸟错误,但是有人能够看到我在这里做错了吗?

我正在尝试按以下格式编译日历数组:

Array
(
[1] => Array
    (
        [14] => Array
            (
                [date] => 14-07-2014     //I ALREADY HAVE THIS INFORMATION, DO NOT NEED TO FIND WITH XPATH
                [title] => Event Title
                [time] => 9:00 AM
                [description] => Some Description
            )
     )

)

我正在使用的HTML是:

<div class="calendar-full">
 <table>
  <tbody>
    <tr>
      <td class="r selected">
        <a>14</a>
        <div class="eventsintable">
          <span class="eventinfo">
             <p class="eventtitle">
               <b>Event Title</b>
             </p>
             <p class="datetimerange"> 9:00AM </p>
             <p class="eventdescription">Some Description</p>
          </span>
       </div>
     </td>
     <td class="l selected">
       <a>15</a>
       ........    // SAME SUB STRUCTURE
     </td>
   </tr>
    <tr>
      <td class="l selected">
        <a>14</a>
        <div class="eventsintable">
          <span class="eventinfo">
             <p class="eventtitle">
               <b>Event Title</b>
             </p>
             <p class="datetimerange"> 9:00AM </p>
             <p class="eventdescription">Some Description</p>
          </span>
          <span class="eventinfo">
            <p class="eventtitle">
              <b>Event Title</b>
            </p>
            <p class="datetimerange"> from 09:00AM </p>
            <p class="eventdescription"></p>
          </span>
       </div>
      </td>
    </tr>
  </tbody>
</table>
</div>

我目前拥有的PHP / XPath:

    $nodes = $xpath->query("//div[@class='calendar-full']/table/tbody/tr//td[contains(@class, 'selected')]");

foreach ($nodes as $node)
{

    $d = 1;

    $today_query = $xpath->query("./a", $node);
    $today = trim($today_query->item(0)->nodeValue);

    $details = $xpath->query("./div[@class='eventsintable']//span[@class='eventinfo']", $node);

    foreach ($details as $detail){

        $eventDate = $today."-".$month."-".$year;

        $title_details = $xpath->query(".//p[@class='eventtitle']", $detail);
        $title = trim($title_details->item(0)->nodeValue);

        $time_details = $xpath->query(".//p[@class='datetimerange']");
        $time = preg_replace( '/\s+/', ' ', trim($time_details->item(0)->nodeValue));

        $desc_details = $xpath->query(".//p[@class='eventdescription']");
        $desc = preg_replace( '/\s+/', ' ', trim($desc_details->item(0)->nodeValue));

        mysql_query("INSERT INTO sas_sws_calendar (clientID, eventMonth, eventDate, eventTitle, eventTime, eventDescription) VALUES ('$clientID', '$month', '$eventDate', '$title', '$time', '$desc')") or die (mysql_error());


        $d++;
    }
}

我非常接近解决这个问题,数组正在形成完美,但数组中的标题,时间和描述字段显示为空白如下:

Array
(
    [14] => Array
        (
            [1] => Array
                (
                    [date] => 14-07-2014
                    [title] => 
                    [time] => 
                    [description] => 
                )

        )

非常感谢任何帮助!!

0 个答案:

没有答案