Re: Subject: making an array from data in textfile

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thanks for all the suggestions.

On 7/24/06, Neil Smith [MVP, Digital media] <php@xxxxxxxxxxxxxxxxxxxxxxxx>
wrote:


>
>Message-ID: <43d6a44c0607221831l7d04b03fw4abb1e14be406c5@xxxxxxxxxxxxxx>
>Date: Sat, 22 Jul 2006 21:31:35 -0400
>From: "Dave W" <dabbaking@xxxxxxxxx>
>To: Php-Db <php-db@xxxxxxxxxxxxx>
>MIME-Version: 1.0
>Content-Type: multipart/alternative;
>         boundary="----=_Part_157142_2227727.1153618295222"
>Subject: making an array from data in textfile
>
>OK, so I have this text file that holds a list of ip addresses like this:
>127.0.0.1,127.0.0.1,127.0.0.1,127.0.0.1,127.0.0.1


Which are strings... You with me OK so far ?
Numeric [float] values only have 1 period.
There's no numeric datatype which has 3.

>Currently, here is my code:
>
>    $file = 'ip.txt';
>    $fh = fopen($file, 'r');
>$theData = fread($fh, filesize($file));
>fclose($fh);
>$ips = array($theData);
>
>Since it's a numeric array,


No, it isn't. It's an array of strings which has numeric indices
(or keys depends what you call em)


>I shouldn't need quotes around the ip octets.


Yes, you should : They're strings unless you use inet_pton to convert
them to numeric values.


>When I try to echo certain parts of the array like:
>
>echo $ips[0];
>
>It's returning the whole array.


If you print_r($ips) you'll see the array structure (View -> Source
if you're looking at it in a browser).
My guess is your lines aren't terminated in \r\n, which PHP uses to
split the lines to array values.

You'll probably have to use some other method such as explode(',',
$ips) to break the individual comma separated entries into a set of
values. Other methods such as file_get_csv are generally more
efficient than reading line by line :
http://uk.php.net/manual/en/function.fgetcsv.php


>  The text file is exactly like an array so it
>shouldn't be a problem.


It's exactly like a CSV, from the example data you provided.
PS, please don't feel the need to prove it by posting the entire log file
;-)

Cheers - Neil








--
Dave W

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux