Reputation: 5
So basically i am using file_get_contents()
function to get the contents of a page. I put those contents in the variable $content
so i can work with it.<p>
On this page the following info is displayed Server (1.7.2) playercount: 400/500
I have to get two pieces of info, the player count on its own (400/500), and the server version (1.7.2). I managed to get the player count with the following code:
$playercount = substr($content, strpos($content, ")") + 1);
that returns ":400/500"
Now here is my problem when i try do the same thing for the server version (1.7.2) I get no return, i presume it is because it doesn't count it as a string:
$version = substr($content, -16, -11);
Can anybody help me?
Upvotes: 0
Views: 1617
Reputation: 76
I did this
preg_match("/^.*?([0-9\.]+).*?([0-9\/]+)/i",$content,$matches);
$playercount=$matches[1];
$version=$matches[2];
Upvotes: 0
Reputation: 2579
You can use regular expression as rabudde suggested. Or, you can get the string between Server (
and )
to find the server version.
Upvotes: 0
Reputation: 7722
Use regex match for both informations:
preg_match('/^\w+\s\((\d\.\d\.\d)\)\s\w+:\s(\d+\/\d+)$/', $content, $result);
print_r($result);
See fiddle http://ideone.com/4vAgdE
Upvotes: 2