Hi. I am trying to search for text on a page. This is the page i am searching: http://www.ace-spades.com/serverlist.json. This is the code I have:

<?php
$link = "http://www.ace-spades.com/serverlist.json";
$linkcontents = file_get_contents($link);

$needle = "ServerIP";
$numofplayers = //I need help here
if (strpos($linkcontents, $needle) == false) {
echo "<center>0/12 Players online</center>";
} else {
echo "<center>$numofplayers/12 Players Online</center>";
}
?>

I need help locating the text after "count": (count is the number of players online the server) and getting the number. The example text is "count": 12. I want to make it so that it displays 12 and not the whole thing. Also I need to locate the example in this:

{"count": 5, "map": "(Server Map)", "uptime": 1436.2805321216583, "cumulative_uptime": 69137.85308456421, "name": "(server name)", "max": 12, "country": "(contry)", "identifier": "(server IP)", "game_mode": "(game mode)", "useless_ping": 99}

I need to locate the server IP, and then locate the "count": in the page. But only displaying the number after "count":. I would be glad if you helped. :D (the reason why I need to locate the server IP is that I don't want it to display random numbers, there are hundreds of servers on the page. The IP of the server is the key to get the amount of players on the server.)

If you use json_decode() you can treat the input as an array:

$d = json_decode($linkcontents,true);
print_r($d);

that outputs:

Array
(
    [count] => 5
    [map] => (Server Map)
    [uptime] => 1436.2805321217
    [cumulative_uptime] => 69137.853084564
    [name] => (server name)
    [max] => 12
    [country] => (contry)
    [identifier] => (server IP)
    [game_mode] => (game mode)
    [useless_ping] => 99
)

and then you can simply: echo $d['count'];

Can anyone else help me?

Sorry, I made an example based on the json string posted above:

$linkcontents = '{"count": 5, "map": "(Server Map)", "uptime": 1436.2805321216583, "cumulative_uptime": 69137.85308456421, "name": "(server name)", "max": 12, "country": "(contry)", "identifier": "(server IP)", "game_mode": "(game mode)", "useless_ping": 99}';

sorry for that ;p I just forgot to be more specific, if you use count() you see that you get 153 arrays, you have to loop them in order to get what you want, for example:

<pre>
<?php
$a = json_decode(file_get_contents('http://www.ace-spades.com/serverlist.json'),true);
$n = count($a);

for($i=0; $i < $n; $i++)
{
    echo 'identifier: ' . $a[$i]['identifier'] .' count: '. $a[$i]['count'] . "\n";
}
?>
</pre>

sorry I am quite new one in PHP

Can you make it so that it displays only the count where this IP is: aos://2252626508:32887?

That identifier is not present in the json file, anyway, just add a if statement inside the loop:

<pre>
<?php
$a = json_decode(file_get_contents('http://www.ace-spades.com/serverlist.json'),true);
$n = count($a);

for($i=0; $i < $n; $i++)
{
    if($a[$i]['identifier'] == 'aos://2252626508:32887')
    {
        echo 'identifier: ' . $a[$i]['identifier'] .' count: '. $a[$i]['count'] . "\n";
    }
}
?>
</pre>

if you want to check a range of identifiers then add an array and change the if statement:

$array = array(
    'aos://2252626508:32887',
    'aos://1379434439:36887',
    'aos://3819391052:32887'
);

if(in_array($a[$i]['identifier'],$array))

bye!

commented: Thank you. This works! I am glad you helped me. Thanks! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.