making an array from data in textfile

making an array from data in textfile

am 23.07.2006 03:31:35 von Dave W

------=_Part_157142_2227727.1153618295222
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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

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, I shouldn't need quotes around the ip octets.
When I try to echo certain parts of the array like:

echo $ips[0];

It's returning the whole array. The text file is exactly like an array so it
shouldn't be a problem.



--
Dave W

------=_Part_157142_2227727.1153618295222--

Re: making an array from data in textfile

am 23.07.2006 03:36:24 von Dave W

------=_Part_157242_5922104.1153618584256
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

OK, well I did a debug and this is whats coming out:

Array
(
[0] => 127.0.0.1,127.0.0.1,127.0.0.1
)

Should I use fgets?

On 7/22/06, Dave W wrote:
>
> 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
>
> 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, I shouldn't need quotes around the ip octets.
> When I try to echo certain parts of the array like:
>
> echo $ips[0];
>
> It's returning the whole array. The text file is exactly like an array so
> it shouldn't be a problem.
>
>
>
> --
> Dave W
>



--
Dave W

------=_Part_157242_5922104.1153618584256--

Re: Re: making an array from data in textfile

am 23.07.2006 14:09:36 von Stut

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dave W wrote:
> 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, I shouldn't need quotes around the ip octets.
> OK, well I did a debug and this is whats coming out:
>
> Array
> (
> [0] => 127.0.0.1,127.0.0.1,127.0.0.1
> )

PHP doesn't know what separates each element of the array, so you need
to tell it...

$ips = explode(',', $theData);

- -Stut
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEw2cA2WdB7L+YMm4RAlmaAJ4+cRJulnESdiZMZ5XtGWZ6Su5TzQCg uSSY
fAqVdvlg4xX+RgrMepUJbc0=
=jI3V
-----END PGP SIGNATURE-----

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: making an array from data in textfile

am 23.07.2006 15:01:22 von Dave W

------=_Part_162652_30224933.1153659682393
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

I figured it out before, but I didn't hit reply all. I used files() and
newlines to create it. Would using explode be a more reliable way?

On 7/23/06, Stut wrote:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Dave W wrote:
> > 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, I shouldn't need quotes around the ip
> octets.
> > OK, well I did a debug and this is whats coming out:
> >
> > Array
> > (
> > [0] => 127.0.0.1,127.0.0.1,127.0.0.1
> > )
>
> PHP doesn't know what separates each element of the array, so you need
> to tell it...
>
> $ips = explode(',', $theData);
>
> - -Stut
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFEw2cA2WdB7L+YMm4RAlmaAJ4+cRJulnESdiZMZ5XtGWZ6Su5TzQCg uSSY
> fAqVdvlg4xX+RgrMepUJbc0=
> =jI3V
> -----END PGP SIGNATURE-----
>



--
Dave W

------=_Part_162652_30224933.1153659682393--

Re: Re: making an array from data in textfile

am 23.07.2006 15:36:37 von Stut

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dave W wrote:
> I figured it out before, but I didn't hit reply all. I used files() and
> newlines to create it. Would using explode be a more reliable way?

I assume you mean the file() function, not files(). The only problem
with doing it that way is that each element of the array will have a
newline on the end. Maybe this is what you want, but I doubt it.

- -Stut
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEw3tl2WdB7L+YMm4RAg3xAJ0SPUPR+00PXVIfJhDfg4WrBWVi/ACg sFOu
9usmbLbhNrXUCef+uCyzAY4=
=RXbe
-----END PGP SIGNATURE-----

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: making an array from data in textfile

am 23.07.2006 16:43:36 von Dave W

------=_Part_164113_4384764.1153665816558
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Well, its what I want so I could do a foreach array like this and print out
the ips in a table:
foreach($ips as $value) {
$value = trim($value);
$domain = gethostbyaddr($value);
echo "


3
$value
$domain ";}
echo '
';

It works!

On 7/23/06, Stut wrote:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Dave W wrote:
> > I figured it out before, but I didn't hit reply all. I used files() and
> > newlines to create it. Would using explode be a more reliable way?
>
> I assume you mean the file() function, not files(). The only problem
> with doing it that way is that each element of the array will have a
> newline on the end. Maybe this is what you want, but I doubt it.
>
> - -Stut
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFEw3tl2WdB7L+YMm4RAg3xAJ0SPUPR+00PXVIfJhDfg4WrBWVi/ACg sFOu
> 9usmbLbhNrXUCef+uCyzAY4=
> =RXbe
> -----END PGP SIGNATURE-----
>



--
Dave W

------=_Part_164113_4384764.1153665816558--