SitePoint Sponsor

User Tag List

Page 3 of 3 FirstFirst 123
Results 51 to 61 of 61
  1. #51
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ok but if we look at the logic of the code, then the repetition of : is less than ;

    hence the delimiter should return ;

    or have i completely misunderstood the logic of the getDelimiter function?


    THanks

  2. #52
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,217
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Okay, try changing
    PHP Code:
    if ($delimitersData === null
    To
    PHP Code:
    if ($delimitersData === null || empty($delimitersData)) 
    That made a difference for me.

  3. #53
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,217
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Oh, and you will need to update the sortDelimiters function to be
    PHP Code:
        // Custom Sort for the Delimiters
        
    public static function sortDelimiters($a$b)
        {
            
    // If the delimiter data for item $a in the array, matches item $b, return 0
            
    if ($a['count'] === $b['count'] && sizeof($a['lines']) === sizeof($b['lines']))
            {
                return 
    0;
            }

            
    // if $a has more lines associated to it than $b, return -1 so it leaves $a higher than $b,
            // otherwise, when $b needs to move up ahead of $a
            
    return sizeof($a['lines']) > sizeof($b['lines']) ? -1;
        } 

  4. #54
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    For the following data, can u pls tell me why its returning , (comma) ?



  5. #55
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Apart from the question in post #54, can u also tell me why does php put extra lines in between array elements when a file is read using the file() function but with the fgets() function it does not add lines?


    Array generated using fgets() function
    Code:
    Array
    (
        [0] => C:\Users\Fabien\Desktop\Combactive\ACTIONS (Réunions et Courriers d'information pour adhérents)\EMAGNY\2012-2013\Médias + Communication\Francophone.txt 21/05/2013 22:35:14
        [1] => Progitek [244 e-mails]
        [2] => aaa.aaa@gmail.ch;
        [3] => aaa.chat.enfant@gmail.fr;
        [4] => aaa@aspas-gmail.org;
        [5] => aaa@gmail.com;
        [6] => aaa@asms-swiss.ch;
        [7] => aaa@gmail.org;
        [8] => aaa@gmail.be;
        [9] => aaa.ellidge@gmail.fr;
        [10] => aaa@gmail.fr;
        [11] => aaa@gmail.be;
        [12] => aaa.wahf@gmail.be;
    )
    Array generated using file() function
    Code:
    Array
    (
        [0] => C:\Users\Fabien\Desktop\Combactive\ACTIONS (Réunions et Courriers d'information pour adhérents)\EMAGNY\2012-2013\Médias + Communication\Francophone.txt 21/05/2013 22:35:14
    
        [1] => Progitek [244 e-mails]
    
        [2] => aaa.aaa@gmail.ch;
    
        [3] => aaa.chat.enfant@gmail.fr;
    
        [4] => aaa@aspas-gmail.org;
    
        [5] => aaa@gmail.com;
    
        [6] => aaa@asms-swiss.ch;
    
        [7] => aaa@gmail.org;
    
        [8] => aaa@gmail.be;
    
        [9] => aaa.ellidge@gmail.fr;
    
        [10] => aaa@gmail.fr;
    
        [11] => aaa@gmail.be;
    
        [12] => aaa.wahf@gmail.be;
    
    )

    Here is the code for putting contents of file in array using fgets() function.
    PHP Code:
        $counter 0;
        while(!
    feof($fp)){
            if (
    $counter == 0) {
            
    $line trimfgets($fp4096) );
            } else {
            
    $line =  fgets($fp4096);

            if(
    mb_detect_encoding($line'UTF-8'true)) {
              
    $line trim$line );

            } else {
              
    $line trimutf8_encode($line ) );

            }
            }

            if (
    strlen($line) != 0) {
              
    $array[] = $line;
             }
            }
            
    $counter++;
          }
            
    fclose($fp); 

    Thanks

  6. #56
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,217
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    For the following data, can u pls tell me why its returning , (comma) ?
    Because the comma and semi-colon are used equally. It doesn't take a preference on first one encountered, so it just returns whichever one is at the top of the array (which happens to be comma).

    If you use the delimiterData to parse your file, it won't matter, as you can then parse the values that are comma delimited and parse the ones that are semi-colon delimited.

    Quote Originally Posted by cancer10 View Post
    Apart from the question in post #54, can u also tell me why does php put extra lines in between array elements when a file is read using the file() function but with the fgets() function it does not add lines?


    Array generated using fgets() function
    Code:
    Array
    (
        [0] => C:\Users\Fabien\Desktop\Combactive\ACTIONS (Réunions et Courriers d'information pour adhérents)\EMAGNY\2012-2013\Médias + Communication\Francophone.txt 21/05/2013 22:35:14
        [1] => Progitek [244 e-mails]
        [2] => aaa.aaa@gmail.ch;
        [3] => aaa.chat.enfant@gmail.fr;
        [4] => aaa@aspas-gmail.org;
        [5] => aaa@gmail.com;
        [6] => aaa@asms-swiss.ch;
        [7] => aaa@gmail.org;
        [8] => aaa@gmail.be;
        [9] => aaa.ellidge@gmail.fr;
        [10] => aaa@gmail.fr;
        [11] => aaa@gmail.be;
        [12] => aaa.wahf@gmail.be;
    )
    Array generated using file() function
    Code:
    Array
    (
        [0] => C:\Users\Fabien\Desktop\Combactive\ACTIONS (Réunions et Courriers d'information pour adhérents)\EMAGNY\2012-2013\Médias + Communication\Francophone.txt 21/05/2013 22:35:14
    
        [1] => Progitek [244 e-mails]
    
        [2] => aaa.aaa@gmail.ch;
    
        [3] => aaa.chat.enfant@gmail.fr;
    
        [4] => aaa@aspas-gmail.org;
    
        [5] => aaa@gmail.com;
    
        [6] => aaa@asms-swiss.ch;
    
        [7] => aaa@gmail.org;
    
        [8] => aaa@gmail.be;
    
        [9] => aaa.ellidge@gmail.fr;
    
        [10] => aaa@gmail.fr;
    
        [11] => aaa@gmail.be;
    
        [12] => aaa.wahf@gmail.be;
    
    )
    I'd assume that fgets is ignoring the \r, \n, or \f indicators, but file() is keeping them.

    Edit:

    Nevermind, it is because you are calling trim() on the line you read during fgets. That is removing the \r, \n, and \f indicators that file() has kept

  7. #57
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you also please comment the code for this post of yours http://www.sitepoint.com/forums/show...=1#post5511703



    PS: Pls dont forget to update the above mentioned code for any updations it might require. For example the changes u made in post #52 (http://www.sitepoint.com/forums/show...=1#post5512773)

    THanks

  8. #58
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,217
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    I've updated #43 (since it was the latest version) accordingly. I also added an edit to all other posts stating those were not the latest version of the code.

  9. #59
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hello again

    I am currently using your code posted in post #43


    One issue is with the following data, the getDelimiter function returns null

    Code:
    EMAIL;
    abc@gmail.com; 
    def@gmail.com;  
    ghi@gmail.com;
    But when I add some chars after semicolon it works like a charm
    Code:
    EMAIL; hey
    abc@gmail.com; 
    def@gmail.com;  
    ghi@gmail.com;

    It didnt resolve even when I added the trim() function.

    Can you please let me know why is this happening?


    Thanks

  10. #60
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,217
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    Hello again

    I am currently using your code posted in post #43


    One issue is with the following data, the getDelimiter function returns null

    Code:
    EMAIL;
    abc@gmail.com; 
    def@gmail.com;  
    ghi@gmail.com;
    But when I add some chars after semicolon it works like a charm
    Code:
    EMAIL; hey
    abc@gmail.com; 
    def@gmail.com;  
    ghi@gmail.com;

    It didnt resolve even when I added the trim() function.

    Can you please let me know why is this happening?


    Thanks
    I don't get that result, I get ';', which is correct....
    See attached code
    csv.zip

  11. #61
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question

    Edit:

    Nevermind, I got the solution. I am using trim() for each line, which removes all whitespaces \n \r etc. Removing them from the code below returns the result that i want...


    Sorry for the late reply.

    Yes you are right, but when I change the code of the __construct function in the csv.php file to the following

    PHP Code:
        public function __construct($file)
        {
            
    $this->filePath $file;
            
    // Read the file contents and store it into a private variable
            
    $fp fopen ($file"r");
            
    $j 0;
            while(!
    feof($fp)){
                if (
    $j == 0) {
                    
    $line trimfgets($fp4096) );
                } else {
                    
    $line =  fgets($fp4096);
                    if(
    mb_detect_encoding($line'UTF-8'true)) {
                      
    $line trim$line );
                    } else {
                      
    $line trimutf8_encode($line ) );
                    }
                }

            if (
    strlen($line) != 0) {
              
    $fileContents[] = $line;
            }
            
    $j++;
          }

          
    $this->fileContents $fileContents;

        } 
    It cannot get the delimiter. I understand that fgets() ignores the \r, \n, or \f indicators, so is there any way to make it detect those indicators in the above code example?


    Thanks


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
  •