0

I'm using the Simple HTML DOM Parser to retrieve a specific div from a website. I remove the part of the div that I don't want by using explode(). I then want to explode the kept part into a new array, but for some reason it doesn't get indexed as intended.

Why doesn't my last row with "echo $content[0];" print "Overall" while "echo $content[5];" does, when Overall is the first string? How do I fix this?

<?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];

?>
4

1 回答 1

0

Between SkillRankLevelXP and Overall, there are 6 spaces, though the browser only shows it as 1. Use the "View Source" menu and you'll see what I mean.

You can use some RegEx to replace 2 or more spaces with just 1 space, and I think that will get you closer to what you want.

<?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);
?>
于 2013-02-23T11:05:43.457 回答