SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot ThetaWaveRider's Avatar
    Join Date
    Aug 2004
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String comparison to line of read file

    I'm chopping up a long SQL file into two files using php to do so (as I don't want to sit here all day selecting text). I'm doing this with the following:

    PHP Code:
    $file_no 1;
    $input file('dbfile.sql');
    $output fopen('output' $file_no '.sql''w');
    $compareline "# Table structure for table `products_description`";
    foreach(
    $input as $line) {
        if(
    strcasecmp($line$compareline) == 0) {
            
    fclose($output);
            
    $file_no++;
            
    $output fopen('output' $file_no '.sql''w');
        } else {
            
    fwrite($output$line);
        }

    The input .sql file is looped though and written to a file line by line until it reaches the text in the $compareline variable, upon which it stops writing to the file and starts writing to a new file. Or at least it does in theory. The problem is with the string comparison. The contents of $compareline are exactly those of one of the lines of the .sql file imported. Why, then, does the string comparison not equal 0 when this line is looped through, thereby starting a new file output? Am I wording something wrong here? Any ideas would be greatly appreciated.

    Thanks,
    TWR

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your input line may have a newline on the end, so try:
    PHP Code:
    foreach($input as $line) {
        
    $line trim($line);
        
    //rest of code here.... 


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
  •