SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a txt file, how do I use PHP to see how many lines that txt file has?

    I have a txt file that looks something like this:

    ,1,AMUR,Amuri Area School,Amuri Area,
    ,3,AGSG,Auckland Girls Grammar,Auck Girls,
    ,6,BURN,Burnside High School,Burnside Hig,
    ,8,CASH,Cashmere High School,Cashmere Hig,
    ,11,CHCO,Christ's College,Christ's Col Christchurch,
    ,12,CBHS,Christchuch Boys,Chch Boys,
    ,13,KEIS,King Edward International King Edward,
    ,15,KHSD,Kings High School,Kings High,
    ,17,SHIR,Shirley Boys High,Shirley Boys,
    ,20,TIMB,Timaru Boys High,Timaru Boys,
    ,25,UNA,Unattached,Unattached,
    ,26,WESL,Wesley College,Wesley Colle,

    I want to see how many lines that file has, also, how can I explode each line by the linebreak into an array and strip the ",num," bit out of each line at the front?

    This is for preparing the data for insertion to MySQL.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you can get the count of the files by using file() which grabs each line of the text file and stores it in an array.

    PHP Code:
    $data file("yourtextfile");

    //prints the number of lines or elements in the array
    print count($data);

    Loop through and explode the line
    foreach($data as $key => $val) {
    $tmp explode(","$val);
    $id $tmp[1];


    Is that what you are after?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually did it another way...but thanks for shoing me file() and count(), didn't know that before.

    Also, I never quite understood things like:

    foreach($data as $key => $val)

    I don't get what that => is for, I've seen them in array() as well, could you please explain what the arrow is for and how to use it?

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Washington State
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at the examples in the manual :

    http://www.php.net/manual/en/control...es.foreach.php

    If we had an array full of keys and values, we can extract them in a look as such :

    foreach ($array as $key => $value) {

    print "$key : $value <br>\n";
    }

    That looks familiar in an array as we could define the array like this :

    $array = array('wa' => 'washington', 'id' => 'idaho');

    Which is similar to :

    $array['wa'] = 'washington';
    $array['id'] = 'idaho';

    So, let's try this :
    PHP Code:
    foreach ($array as $state_abbreviation => $state_name) {

      print 
    "$state_abbreviation : $state_name <br>\n";

    It'll loop through all the $array contents. Foreach is pretty cool, and pretty.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •