Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Php reading file and removing certain data

    Need to try and simplify my code possibly into two functions and I want to be able to use substr and strpos and store them into an array?


    Here's the code so far, essentially I want to. E able to take the text file that has numbers and commas and remove the commas, not happy with it would rather it be simpler in a away but with out the use of explode. Any help would be brilliant. Thank you




    PHP Code:
    <?php
    //linking table to form
    $studentid=$_POST["id"];

    //To print out student ID
    echo "You have selected Student ID: $studentid <br />";


    //define what the file equals to which is the students ID
    $file "$studentid.txt";

    //Opening the selected files
    $fileopen fopen($file,'r') or die("Sorry, cannot open $file");
    if(
    $fileopen){

    while ((
    $buffer=fgets($fileopen,4096))!==false){
    // echo $buffer."<br/>";
    }*
    if(!
    feof($fileopen)){
    echo 
    "Error:unexpected fgets() fail\n";

    }*
    fclose($fileopen);*
    }

    //remove all commas from txt file
    $filearray=file("$studentid.txt");
    $count=count($filearray);

    for(
    $i=0$i<$count$i++){

    $startnum=0;
    $stringlength strlen($filearray[$i]);
    $comma strpos($filearray[$i], ","$startnum);
    $array[$i][0] = substr($filearray[$i], $startnum$comma);
    $j=1;

    while (
    $j<$count-1){
    $comma strpos($filearray[$i], ","$startnum);
    $comma2 strpos($filearray[$i], ","$comma+1);
    $sub $comma2-$comma;
    $num substr($filearray[$i], $comma+1$sub -1);
    $startnum $comma2;
    $array[$i][$j]= $num;

    $j++;
    }
    $stop $stringlength $comma2;
    $array[$i][$j] = substr ($filearray[$i], $comma2 1$stop);
    }

    function 
    array_transpose($filearray)
    {
    array_unshift($filearrayNULL);
    return 
    call_user_func_array('array_map'$filearray);
    }

    //table heading
    echo"<h1>The Table</h1>";

    //creating the rows and columns for the txt file
    echo "<table border = 1 >";
    for(
    $row=0$row<$count$row++){
    print 
    "<tr>";

    for(
    $col=0$col<$count$col++){
    print 
    "<td>".$array[$row][$col]."</td>";
    }*
    }
    echo 
    "</table>";



    //table heading
    echo"<h1>Transpose</h1>";

    ?>
    *



    Sent from my F5321 using Tapatalk
    Last edited by vinyl-junkie; Dec 1st, 2017 at 12:26 AM. Reason: added code tags


 

Tags for this Thread

Posting Permissions

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