SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Weird Little Girl Desdelena's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using a string instead of bytes.

    I want to start writing to a file at a particular position. I was told to use something like this.

    PHP Code:
    $findStart fseek($fileOpen,$fileByes); 
    Where $fileOpen is what opens the file, and $fileBytes is the number of bytes into the file that I want to start writing. But that will not work because I want to be able to alter things in the file after Ive got my script functioning. So I just want it to look for a <!-- Begin --> comment and start writing after that. How exactly do I go about that? This is what Ive already tried... and I dont think its working because if I write something to the file once it works. If I go to write something else, which should be added to the existing file, it overwrites the file with the new content.

    PHP Code:
    $startMarker "<!-- Begin -->";
    $openFile fopen("entries.html","r+");
    $findStart fseek($openFile,$startMarker);
    $writeFile fwrite ($openFile,$entry);
    $closeFile fclose($openFile); 

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is (or should be) a more simple way to do this, but this is the one that comes to mind... so you'll have a backup if no one else posts:

    Code:
    $filename = "test.txt";
    $fd = fopen ($filename, "r+");
    $contents = fread ($fd, filesize ($filename));
    preg_match('/([\d\D]*)(<!-- begin -->)([\d\D]*)/', $contents, $sections);
    $stuff_to_add = "<br><br>boogie boogie<br><br>";
    $contents = $sections[1]; // the part of the file before "begin"
    $contents .= $stuff_to_add; // stuff to add
    $contents .= $sections[2]; // <!-- begin -->
    $contents .= $sections[3]; // part of file after "begin"
    $fwrite($fd, $contents);
    fclose ($fd);
    You can change the order of the $contents = $sections lines to arrange the file as you like.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    Weird Little Girl Desdelena's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah samsm to the rescue again. Same function too. You know that really probably should have clicked in that I could do that! The last solution you gave me worked great btw. Im off to test this out now.

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it involves manipulating text, it can be done with regular expressions.

    However, if there is a built-in function that is designed to take care of your task, that is probably better.

    I'm sure there is some combination in the fopen, fread, (etc) family of functions that can handle what you want and if you were dealing with quite a bit of data I would expect that it would be more practical to use those functions. Of course, using what you know is practical, which is why I often come running to regular expressions.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  5. #5
    Weird Little Girl Desdelena's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... this doesnt seem to be working. Perhaps Ive made a typo?

    PHP Code:
    $fileName "entries.html";
    $openFile fopen($fileName,"r+");
    $fileContent fread ($openFile,filesize($fileName));
    preg_match('/([\d\D]*)(<!-- begin --> )([\d\D]*)/',$fileContent,$section);
    $fileContent $section[1]; //File content before marker.
    $fileContent .= $section[2]; //The file marker.
    $entry "<br><table border=0 cellpadding=3 cellspacing=3 width=75% align=\"center\" class=\"boxed\">
    <tr valign=\"middle\">
    <td align=\"left\"><b><a href=\"mailto:
    $email\">$name</a></b> from $country  |  <a href=\"$website\">$website</a></td>
    <td align=\"right\"><span class=\"small\">
    $submittedOn</span></td>
    </tr>
    <tr align=\"left\" valign=\"top\">
    <td colspan=2>
    $comments</td>
    </tr>
    <tr align=\"right\" valign=\"middle\">
    <td class=\"small\" colspan=2>
    $ip  |  $browser $version</td>
    </tr>
    </table>"
    ;
    $fileContent .= $entry//The entry to be added.
    $fileContent .= $section[3]; //File content after marker.
    $writeFile fwrite ($openFile,$fileContent);
    $closeFile fclose($openFile); 
    I dont get any warnings or errors... but when I go to my entries.html page it hasnt done anything to it. Ive tried refreshing.

  6. #6
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've learned recently that
    $variable = $one
    $variable .= $two
    $variable .= $three
    etc.
    ...is not the best programming practice. This is better (supposedly uses less memory):
    $variable= $one . $two . $three
    Not a huge thing.

    Also, the vb [php] tags mess up regular expressions... primarily the backslashes.

    Anyway....

    The part of the script that searches and sorts seems to work fine. Try adding this to the end of the script to see if it is actaully working on your files:
    echo($fileContent);

    If that displays the correct info then the problem is in writing the file... if not the problem is in opening or altering the file. Try that out and we'll go from there.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  7. #7
    Weird Little Girl Desdelena's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip. I will keep that in mind. I think for now its easier for me to understand it if they are on seperate lines though.

    I echoed the $fileContent after the $closeFile line and it gave me a page with the new content, placed as it should be, added to the original content of the entries.html page. However when I went to the file that holds the entries, only the original content was there even after refreshing. The writing function worked fine before, and I havent changed a thing there. Now Im really confused.

  8. #8
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that narrows it down at least... the file is opening and reading alright... but seems it is not being altered by fwrite.

    Is it possible php doesn't have write permissions on the file you are using? I would expect an error from that, though.

    If that's not it, I have one more guess which can be tested by closing the file after reading and re-opening it for writing:
    PHP Code:
    $fileName*=*"entries.html";

    $openFile*=*fopen($fileName,"r");

    $fileContent*=*fread*($openFile,filesize($fileName));

    $closeFile*=*fclose($openFile);

    preg_match('/[put regular expression here]/',$fileContent,$section);

    $fileContent*=*$section[1];*//File content before marker.

    $fileContent*.=*$section[2];*//The file marker.

    $entry*=*"entry text";

    $fileContent*.=*$entry;*//The entry to be added.

    $fileContent*.=*$section[3];*//File content after marker.

    $openFile*=*fopen($fileName,"r+");

    $writeFile*=*fwrite*($openFile,$fileContent);

    $closeFile*=*fclose($openFile); 
    See if that change in code works.

    I agree that the commented, separate line version is easier to read. Something like this would work:

    PHP Code:
    $variable$one //comment
             
    $two  //comment
             
    $three //comment 
    But like I said, not too important. Just thought I'd note it since I used it.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #9
    Weird Little Girl Desdelena's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oohh! Just like magick. I have to refresh the page, but thats only expected because its cached. Why is it that you have to reopen the file before it will write to it? I think I may have found something that can be used instead of preg_match(). Its explode(), but I really dont know how to use that at all. I will probably stick to this method seeing as it already works!

  10. #10
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm assuming...
    the re-opening resets the internal file pointer which is brought to the end of the file by fread.

    ....but I'm not sure.

    You're right, explode would work...the only difference is that it would make more array elements for additional occurances of <!--begin-->.

    With the preg we've been using...

    This will always use only last instance of <!--begin-->:
    preg_match('/([\d\D]*)(<!-- begin --> )([\d\D]*)/', $content, $section);

    And this would always use just the first:
    preg_match('/([\d\D]*?)(<!-- begin --> )([\d\D]*)/', $content, $section);
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


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
  •