I have a text file named ip_array.txt, which contains the following: '192.168.1.1', '127.0.0.1', '72.65.2.1' Code (markup): I'm trying to place the following into an array using the following code: $fh = fopen('ip_array.txt', 'r'); $ip_array = array(fgets($fh)); fclose($fh); print_r ($ip_array); PHP: the output is: Array ( [0] => '192.168.1.1', '127.0.0.1', '72.65.2.1' ) HTML: Which is not what I want. I want something like the following: $ip_array = array('192.168.1.1', '127.0.0.1', '72.65.2.1'); print_r ($ip_array); PHP: which outputs the following Array ( [0] => 192.168.1.1 [1] => 127.0.0.1 [2] => 72.65.2.1 ) HTML: How would I get it to store the contents from the text file into an array so that it outputs what's on the second example? Thanks beforehand!
$fh = fopen('ip_array.txt', 'r'); $ip_array = array(fgets($fh)); fclose($fh); $ip_array = explode(",", $ip_array); //explodes into an array //it seperates the string by any and all commas print_r ($ip_array); Code (php):
You'll have a much easier time if you store entries on new lines instead of the format you have. 192.168.1.1 127.0.0.1 72.65.2.1 Code (markup): Then getting your array is as simple as this, $my_array = file('ip_array.txt'); Code (markup):
Be sure to trim($my_array[$i]) before using it, the newlines are still going to be attached to the end of each array element.