Splitting strings in file to array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomcatf14
Forum Newbie
Posts: 11
Joined: Wed Sep 05, 2007 3:33 pm

Splitting strings in file to array

Post by tomcatf14 »

Hi,

I would like to split each strings to an array on a multiple lines file. The file should look like as below:-

------------------------------
Filesystem 1K-blocks Used Avail Capacity Mounted on
/dev/ad0s2a 507630 190196 276824 41% /
devfs 1 1 0 100% /dev
/dev/ad0s2e 507630 20 467000 0% /tmp
/dev/ad0s2f 10154158 5673658 3668168 61% /usr
/dev/ad0s2d 6090094 4987298 615590 89% /var
------------------------------

How do i achieve that? I can use preg_split to split strings in a line to its array but haven't figure out on how to do it on multiple lines file. Each of the string above need to be assign to an array. Better still if there is something like coordinate to help. Eg. "array[1] = (row 2, col 2)"
Any help?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

file() and explode() might help.
tomcatf14
Forum Newbie
Posts: 11
Joined: Wed Sep 05, 2007 3:33 pm

Post by tomcatf14 »

Everah wrote:file() and explode() might help.
How i do use explode for non consistent white space across the file? Letz say like this:-

----------------------
Filesystem 1K-blocks Used Avail Capacity Mounted on
tomcatf14
Forum Newbie
Posts: 11
Joined: Wed Sep 05, 2007 3:33 pm

Post by tomcatf14 »

Problem solved by using line count and loop array.
Post Reply