2013-02-23 49 views
0

我正在使用簡單的HTML DOM解析器從網站檢索特定的div。我使用explode()刪除了我不想要的div部分。然後我想將保留的部分分解成一個新的數組,但由於某種原因,它不會按照預期進行索引。爆炸兩次沒有正確索引

爲什麼我最後一行沒有「echo $ content [0];」打印「總體」而「echo $ content [5];」當Overall是第一個字符串時呢?我該如何解決?

<?php 
    include_once('simple_html_dom.php'); 

    $html = file_get_html('http://services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=Pur'); 
    $content = $html->find('div[id=contentHiscores]', 0)->plaintext; 
    echo $content; 
    echo "<br><br><br><br>"; 


    $content = explode("SkillRankLevelXP", $content); 
    $content = $content[1]; 
    echo $content; 
    echo "<br><br><br><br>"; 


    $content = explode(" ", $content); 
    echo $content[0]; 

?> 

回答

0

SkillRankLevelXP與總體之間,有6位,儘管瀏覽器只顯示它作爲1.使用「查看源文件」菜單,你會明白我的意思。

你可以使用一些正則表達式來替換2個或更多的空格,只有一個空格,我認爲這會讓你更接近你想要的。

<?php 
    include_once('simple_html_dom.php'); 

    $html = file_get_html('http://services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=Pur'); 
    $content = $html->find('div[id=contentHiscores]', 0)->plaintext; 
    $content=preg_replace('/ {2,}/', ' ', trim($content)); 

    $content = explode('SkillRankLevelXP ', $content); 
    $content = $content[1]; 

    $content = explode(' ', $content); 
    print_r($content); 
?>