刮擦表 - 根据奇数或偶数从TR获取不同的数据

时间:2015-08-03 10:32:12

标签: javascript php

我正在试图刮一张桌子,但我有一个问题,数据是不同的,这取决于它是奇数行偶数行

行的示例如下所示:

<tr>
   <td>
      1<sup>st</sup>                        (9)                    
   </td>
   <td></td>
   <td class="ixt">
      <strong class="name">
      3                            <a href="/racing/profiles/horse/897171/fleeting-visit">
      Fleeting Visit                            </a>
      </strong>
   </td>
   <td class="ixt"><a href="/racing/profiles/trainer/43295/eve-johnson-houghton" class="name">Eve Johnson Houghton</a></td>
   <td>2</td>
   <td>9-5                                            </td>
   <td class="ixt">
      <a href="/racing/profiles/jockey/77823/j-p-fahy" class="name">J P Fahy</a>
   </td>
   <td class="mobile-hdn">
      9/4f                                            
   </td>
</tr>

<tr class="note">
   <td colspan="8" data-mobile-colspan="7">
      <strong class="desktop-hdn">SP 9/4f</strong> Chased leaders, led over 2f out, stayed on well, eased towards finish opened 2/1                        
   </td>
</tr>

在第一个TR我希望得到马的名字(Fleeting Visit)和第二个TR我希望得到评论(追逐领导者,领先2f出局,保持良好状态,顺利完成开场2 / 1。

每匹马都会这样,所以任何奇数行都是马的名字,任何偶数行都是评论。我试图按如下方式运行脚本,但这需要很长时间

foreach($getcommentdetails2 as $getcommentdetails22)
{   
    $count = 0;
    foreach($getcommentdetails22->childNodes as $node)  
        if(!($node instanceof \DomText))      
            $count++;

    for ($i = 0; $i < $count; $i++)
    {
        if ($i % 2 == 0)
        {
            echo $horsecomment = strtolower(trim(preg_replace("/[^A-Za-z ]+/", "", preg_replace("/\([^\)]+\)/","",trim($getcommentdetails22->childNodes->item($i)->childNodes->item(4)->nodeValue)))));
        }
        else
        {
            echo  $comment = $getcommentdetails22->childNodes->item($i)->nodeValue;
            // $results = $db->query("UPDATE Race_Records SET comments='$comment',Horse2 ='$horsecomment'   WHERE Horse='$horsecomment'");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

循环{ }中需要

foreach($getcommentdetails22->childNodes as $node)

foreach($getcommentdetails2 as $getcommentdetails22)
{   
    $count = 0;
    foreach($getcommentdetails22->childNodes as $node)  
    {
        if(!($node instanceof \DomText))      
        $count++;

        for ($i = 0; $i < $count; $i++)
        {
            if ($i % 2 == 0)
            {
                echo $horsecomment = strtolower(trim(preg_replace("/[^A-Za-z ]+/", "", preg_replace("/\([^\)]+\)/","",trim($getcommentdetails22->childNodes->item($i)->childNodes->item(4)->nodeValue)))));
            }
            else
            {
                echo  $comment = $getcommentdetails22->childNodes->item($i)->nodeValue;
                // $results = $db->query("UPDATE Race_Records SET comments='$comment',Horse2 ='$horsecomment'   WHERE Horse='$horsecomment'");
            }
        }
    }
}