Integer Set into Integer Array

Hello!

My goal is to make a dice roller that takes numbers from random.org, rerolls any dice that are above a certain number, and finally records them.

So far the only problem I am having is figuring out how to get the numbers from the format they’re in (http://www.random.org/integers/?num=10&min=1&max=10&col=1&base=10&format=plain&rnd=new) into an array of integers, since they’re all presented at once and aren’t a string or anything.

How could I do this?

They’re separated by new lines. The character(s) for new lines varies based upon the operating system that they were generated on, so it might be best to test it first.

To split up a string into an array, you use the explode function:

http://uk3.php.net/manual/en/function.explode.php

[php]$raw = file_get_contents(‘http://www.random.org/integers/?num=10&min=1&max=10&col=1&base=10&format=plain&rnd=new’);

$raw_numbers = explode("\n", $raw); // Produces an array of 11 items as random.org places an extra back space

unset($raw_numbers[10]); // Remove the extra empty item

print_r($raw_numbers); // Display the numbers[/php]

The file_get_contents function was used to retrieve the data. Some hosts do not allow this to be used for remote URLs, so it may be worth having a look at cURL (if you cannot use file_get_contents). Also, the unset function (if you did not know) removes a variable from existence - the opposite of defining it.

Thank you ^ ^

Sponsor our Newsletter | Privacy Policy | Terms of Service