[PHP help]How to read row of integer from simple text file?

How can I read integers from a text file and store them in an array.

I have the following text file.

A B C -3 -2 -1 0 1 2 3 11 -3

The above code consists of 9 integers which are -3,-2,-1,0,1,2,3,11,-3.
Its a .txt file.

I wanna read that file using php code which stores all those integers (only integers mentioned above) in an array.

I have been trying to figure out since 3 days and now I am out of suggestions so I need help, would be really grateful to this community, thanks.

More explanation:
It should be as follows,
$somearray[0]=-3
$somearray[1]=-2
and so on…
.
.

Hi xzax,

This should do what you are looking for:[php]preg_match_all(’/[-0-9]+/’,$array,$matches);[/php]

Replace $array with the actual name of your array. $matches will be an array with all of the integers.

Here is a simple example:[php]<?php
$array = ‘A B C -3 -2 -1 0 1 2 3 11 -3’;
preg_match_all(’/[-0-9]+/’,$array,$matches);

echo ‘

’;
print_r($matches);
echo ‘
’;[/php]

The output is:[code]Array
(
[0] => Array
(
[0] => -3
[1] => -2
[2] => -1
[3] => 0
[4] => 1
[5] => 2
[6] => 3
[7] => 11
[8] => -3
)

)[/code]

Wow thanks a lot!

I had been trying to figure out like hell. Thanks, I registered in this community as I am a student of college, and this community is what I was looking for.

Awesome, glad it worked and welcome to the community!

Sponsor our Newsletter | Privacy Policy | Terms of Service