jeudi 26 mai 2016

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




Aucun commentaire:

Enregistrer un commentaire