splitting text file contents into seperate arrays.

Discussion in 'PHP' started by derek34, Nov 13, 2007.

  1. #1
    I need help splitting the contents of a text file into an array. I tried using the split() function. However, that would not work because the file is already an array...
    Each line of the text file is: name,phonenumber,email,website

    I need to split each of the words between the commas into arrays. I don’t know how you would split the file contents if the contents is already in an array. For example I’ve got so far:

    $selectedfile = "./courses/".$_GET['course'].".txt";
    $openfile = file($selectedfile); //this works
    $splitfile = split(",",$openfile); //this doesn’t. $openfile is already an array.

    Also, after I get each word of each line split into an array, how would I put the array contents into an HTML table. Would it be as simple as just doing:

    echo “<table>”;
    foreach($arrayname as $tablearray)
    {
    echo “<tr><td>$tablearray</td></tr>”

    }
    echo “</table>”


    Thanks
    - Derek
     
    derek34, Nov 13, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You can do it like this:
    
    $selectedfile = "./courses/".$_GET['course'].".txt";
    $openfile = file($selectedfile); //this works
    
    foreach ($openfile AS $line)
    {
    	// name,phonenumber,email,website
    	list($name, $phone, $email, $website) = explode(',', $line);
    
    	echo "<tr><td>Name: {$name}</td></tr>\n";
    	// Etc...
    }
    
    PHP:
     
    nico_swd, Nov 13, 2007 IP
  3. tonyrocks

    tonyrocks Active Member

    Messages:
    1,574
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    88
    #3
    text manipulation is server intensive :)
     
    tonyrocks, Nov 13, 2007 IP