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

时间:2013-06-08 08:46:56

标签: php regex

如何使用类'evenRowIndxView'在每个块中使用类'StockItem'解析PHP中的每个第五个表达式?

源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>  
     

等...

2 个答案:

答案 0 :(得分: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%准确,但你应该从中得到这个想法。

答案 1 :(得分: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>";
    }

}

应该在evenRowIndxView的第5行打印,在您给定的样本中应该打印:

32,6800 ( +1,37%)