解析每个块中的第五个表达式


Parsing every fifth expression in every block

我如何在php中解析类'StockItem'在每个块'evenRowIndxView'第五表达式?

源html有几个'block'类'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>  

等等……

这就是一个简单而肮脏的正则表达式解决方案:

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>";
    }
}

应该在evenRowIndxView中每隔第五行打印一次,在给定的示例中,它应该打印:

32,6800 ( +1,37%)

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

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

现在对StockItem

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

}

假设你只需要value

$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%准确,但你应该从中得到一个想法。