SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    PHP fwrite/fread problem

    Hi there,

    I have a slight problem here when reading and writing to a text file.

    When I read from the text file (which has a keyword on each new line), I explode() the content of the text file to retrieve an array of keywords. For some odd reason when I write the LAST keyword back to a text file, it adds a strange blockish character to the end.

    When I output this same LAST keyword in the array, which was also the keyword on the last line of the text file, it just removes the space created by the newline.

    Sounds confusing...
    Anyhow...the blockish character is the problem.
    This is probably some encoding issue, but I can't seem to figure it out. I've had this problem before, but couldn't fix it.

    Does someone know how to prevent this?

  2. #2
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea

    But you could try using the ord() funcyion to work out exactly what teh character is!

    http://uk.php.net/manual/en/function.ord.php
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  3. #3
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok...

    I have a '.txt' file with 9 keywords in it. Each starting with "keyword" followed by a number, from 1 to 9. Example :

    keyword1
    keyword2
    keyword3
    keyword4
    keyword5
    keyword6
    keyword7
    keyword8
    keyword9
    I upload this file with my script and use explode() to get an array of keywords. For each array key, I then insert the value for that key into a db table, each on a new row.

    ...then I try to write these keywords to a text file. Note that it might not be the same keywords, and they won't be in the same order, etc... since I have uploaded this file several times, meaning I might have 18, 27, 36, etc... keywords in the database.

    See below :



    I'm trying to write each keyword on a newline into a text file. Where the last keyword of each file that was uploaded is written (keyword9 in this case), it doesn't place a newline, but rather a blockish character.

    Here is some sample PHP code of what I used :

    PHP Code:
    <?php

    $fh 
    fopen("word1.txt""a"); //"a" for append at the end

    foreach ($keywords as $keyword)
    {
        
    fwrite($fh$keyword "\n");
    }

    fclose($fh);

    ?>
    I'm so frustrated. Like I said before, I think this is some encoding problem, but I don't know how to strip it out or prevent it.

    I'm running PHP 5.1.6 on Apache server on Windows XP OS as a local server.

  4. #4
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I've sorted out the problem...

    When typing the file with the keywords for uploading, I need to hit enter/return after the last keyword. This seems to get rid of the 'blockish' character completely.

    Question is...why is it doing this?


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
  •