-2

如何在 php 中解析每个带有“evenRowIndxView”类的块中带有“StockItem”类的第五个表达式?

源 html 有几个带有“evenRowIndxView”类的“块”:

<tr class="evenRowIndxView"   onclick="document.location = 'wmquerysnew.asp?refID=12105397&deststamp=59'">

  <td class="StockItem"    align='center'   >12105397</td>
  <td class="StockItem"  nowrap  align='right'   >100,00</td>
  <td class="StockItem"  nowrap  align='right'   >3268,00</td>
  <td class="StockItem"  nowrap  align='right'   >0,0305</td>
  <td class="StockItem"  nowrap  align='right'   >32,6800 ( +1,37%)</td>
  <td class="StockItem"  nowrap  align='right'   >199,5</td>
  <td class="StockItem"  nowrap  align='right'   >6519,64</td>
  <td class="StockItem"  nowrap  align='right'   >08.06.2013 12:11:36</td>

</tr>

<tr class="oddRowIndxView"   onclick="document.location = 'wmquerysnew.asp?refID=12105391&deststamp=57'">

  <td class="StockItem"    align='center'   >12105391</td>
  <td class="StockItem"  nowrap  align='right'   >90,85</td>
  <td class="StockItem"  nowrap  align='right'   >2968,96</td>
  <td class="StockItem"  nowrap  align='right'   >0,0305</td>
  <td class="StockItem"  nowrap  align='right'   >32,6798 ( +1,37%)</td>
  <td class="StockItem"  nowrap  align='right'   >99,5</td>
  <td class="StockItem"  nowrap  align='right'   >3251,64</td>
  <td class="StockItem"  nowrap  align='right'   >08.06.2013 12:04:41</td>

</tr>  

ETC...

4

2 回答 2

1

那里有一个简单而肮脏的正则表达式解决方案:

if(preg_match_all("/<tr[^>]+evenRowIndxView[^>]+>(\s*<td[^>]+>[^<]+<\/td>\s*){4}\s*<td[^>]+StockItem[^>]+>([^<]+)<\/td>/i", $str, $matches))
{
     //print_r($matches);
    foreach($matches[2] as $match)
    {
        echo $match."<br>";
    }

}

应该每 5 行打印一次evenRowIndxView,在您给定的示例中它应该打印:

32,6800 ( +1,37%)
于 2013-06-08T09:22:05.003 回答
1

首先,您需要隔离所有evenRowIndxView块。我会用爆炸

$blocks = explode("evenRowIndxView", $html);

现在做同样的事情StockItem

foreach ($blocks as $block)
{
   $item = explode("StockItem", $block);
   //now your item should be at $item[4]


}

假设你想要的只是价值

$str = '<td class="StockItem'.$item[4]; //this put back some HTML so it can be later removed with strip_tags
$value = strip_tags($str);

上面的代码可能不是 100% 准确,但你应该从中得到想法。

于 2013-06-08T08:54:37.710 回答