Nippledisaster
Nippledisaster

Reputation: 298

Php parse html dom and target elements

I have a gaming server and i want to build a simple php to get my server players because there is no API to use

So after a little search i have found this Simple php DOM Parser And this is perfectly suitable for what i want to do :)

Lets say my server link contains this structure

<div id="server-page-info">

<div class="row-tight">
    <div class="box span1">
        <header>
            <h1>Players</h1>
        </header>
    </div>
    <div class="box span1">
        <header>
            <h1>Ping</h1>
        </header>
    </div>
</div>
<div class="row-tight">
    <div class="box span1">
        <section class="box-content">
            <h5>
                0 / 64
            </h5>
        </section>
    </div>
    <div class="box span1">
        <section class="box-content">
            <h5>
                -ms
            </h5>
        </section>
    </div>
</div>

</div>

Lucky me, there are a bunch of row-tight and box and box-content class names :)

So i want to get the value of one of this box-content sections which is shows 0 / 64 above

My php code

Sorry im a newbie on PHP. you can also see how i have targeted the element but it does not work and i do not get anything :)

require 'simple_html_dom.php';
$url = '';
$html = file_get_html($url);

$player = $html->find('div[id=server-page-info] .row-tight:nth-child(2) .box:first-child');

foreach( $player as $players ){

    echo $players->plaintext;

    }

I will also add one more question :) Now when i get the server players value as plaintext i want to wrap it in a div :)

Thanks

Upvotes: 0

Views: 1282

Answers (1)

Gvidas
Gvidas

Reputation: 1964

From Simple html DOM manual i don't see that it supports nth-child, but it supports other syntax: $html->find('<element>', <nth-child>).So your code should look something like this:

$player = $html->find('div[id=server-page-info]')->find('.row-tight',1)->find('.box',0);

And i suggest to use # for ids instead of attributes:

$player = $html->find('#server-page-info')->find('.row-tight',1)->find('.box',0);

UPDATE

If you want to do next find you need always select only one element with find('<element>', <nth-child>) In your case: $player = $html->find('#server-page-info',0)->find('.row-tight',1)->find('.box',0); .

I have tested piece below and it displays 0 / 64.

require 'simple_html_dom.php';
$html = str_get_html('<div id="server-page-info">
<div class="row-tight">
    <div class="box span1">
        <header>
            <h1>Players</h1>
        </header>
    </div>
    <div class="box span1">
        <header>
            <h1>Ping</h1>
        </header>
    </div>
</div>
<div class="row-tight">
    <div class="box span1">
        <section class="box-content">
            <h5>
                0 / 64
            </h5>
        </section>
    </div>
    <div class="box span1">
        <section class="box-content">
            <h5>
                -ms
            </h5>
        </section>
    </div>
</div>
</div>');
$player = $html->find('#server-page-info',0)->find('.row-tight',1)->find('.box',0);
echo $player->plaintext;

Upvotes: 1

Related Questions