2016-08-02 63 views
2

我使用PHP简单HTML DOM Parser从网站刮一些值。 我已经拆分了一个名为$results(格式为:number:number) 使用.str_replace的变量,但我需要分别使用$results中的这两个数字。 这是我的代码:PHP简单的HTML DOM分析器与值拆分

require_once '../simple_html_dom.php'; 

$html = file_get_html('http://www.betexplorer.com/soccer/belgium/jupiler-league/results/'); 

$match_dates = $html->find("td[class=last-cell nobr date]"); // we have 1 per match 
$titles = $html->find("td[class=first-cell tl]"); // 1 per match 
$results = $html->find("td[class=result]"); // 1 
$best_bets = $html->find("td[class=odds best-betrate]"); // 1 
$odds = $html->find("td[class=odds]"); // 2 

$c = $b = 0; // two counters 

foreach ($titles as $match) { 
    echo $match_dates[$c]->innertext." - ".$match->innertext." ".str_replace(':',' ',$results[$c]->innertext)." - ".$best_bets[$c++]->attr['data-odd']."/".$odds[$b++]->attr['data-odd']."/".$odds[$b++]->attr['data-odd']."<br/>"; 
} 

所以我需要单独从$results使用这两个数字,我想所有的值插入到table
谢谢

+1

'名单($ NUM1,$ NUM2)=爆炸( ':',$结果[$ C] - >的innerText);' – splash58

+0

我试图实现它,但我得到的 “阵列”,而不是数字。 ..我可以在我的代码中实现它吗? – Marci

+0

发布'var_dump($ results [$ c] - > innertext)''和您当前的代码。 – Jakumi

回答

1

由于@ splash58在评论中已经提到,所以您必须使用explode来轻松区分这两个值。

foreach ($titles as $match) { 
    list($num1, $num2) = explode(':', $results[$c]->innertext); // <- explode 
    echo $match_dates[$c]->innertext . 
     " - ".$match->innertext." ".$num1.':'.$num2 .   // <- example use 
     " - ".$best_bets[$c++]->attr['data-odd'] . 
     "/".$odds[$b++]->attr['data-odd'] . 
     "/".$odds[$b++]->attr['data-odd'] . 
     "<br/>"; 
} 
+0

感谢您的回答。我得到一个错误:“警告:爆炸()期望至少2个参数,” – Marci

+0

哎呀,对不起,我忘了爆炸线中的“:” – Jakumi

+0

它的工作!谢谢! – Marci