SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question need help on writing to text file

    Hi,
    I need help on writing to a new text file or existing file,
    I want to open a text file, count the words, add some data to it after certain number of words from another file (csv file) then make a new text file of the result or just write back to the same text file,
    I don't know how to proceed, here is what I have so far, I added
    function file_put_contents,
    and i get this error:
    Fatal error: Call to undefined function: file_put_contents()

    host server got php ver 4.4.7

    appreciate your help.

    PHP Code:

    <?php 
     
    $count 
    $oldcount $totalcount $tally 0;
    $result "";

    $totalwords file_get_contents("content.txt");
    $data file_get_contents("abc.csv");
    $data explode('%%'$data); 


    $di 0;
    $totalwords strip_tags($word);
    $totalwords preg_replace'/ +/'" "$totalwords);
    $totalwords explode (" "$totalwords);
    $totalwords count ($totalwords);

                  
                for(
    $x=0$x $count$x++)
                    {
                    
    $tally++;
                    
    $result .= $totalwords[$x] . " ";    
            if(
    $tally == 65 )
        {
                    
    $result .= "\n" $data[$di++];
            if(
    $di>=count($data))$di=0;
                    
    $tally 0;} 
                    
    $totalcount += $count;}

    file_put_contents('content-2.txt',$result);
                 
    ?>

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    file_put_contents() requires php 5. you'll have to use fopen() and fputs().

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for reply;

    I replaced:

    $totalwords = file_get_contents("content.txt");

    with:

    $totalwords = fopen("content.txt", "w");


    and replaced:

    file_put_contents('content-2.txt',$result);

    with :
    fwrite($result);

    I get now this error:


    Warning: fwrite(): supplied argument is not a valid stream resource in home/AAA/public_html/textcount/count3.php on line 32

    online 32 there is:fwrite($result);

    I don't know how to handle fwrite.

  4. #4
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    file_get_contents() is supported by php 4, so switch that back.

    have you read the manual pages for fopen() and fwrite()? you have to fopen() before you can fwrite().

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for reply;
    after trying many variation of fwrite and get errors, I took off the fwrite, and just tried the script without it, to see if it can echo the result on the page,
    it seems the first part does not work either,
    when page loaded no error , nothing, blank page,
    I expected to see text with some data inserted to it,
    any idea?

    here is what I tried:

    PHP Code:

    <?php 
     
    $count 
    $oldcount $totalcount $tally 0;
    $result "";

    $totalwords file_get_contents("content.txt");
    $data file_get_contents("abc.csv");
    $data explode('%%'$data); 

    $di 0;
    $totalwords strip_tags($word);
    $totalwords preg_replace'/ +/'" "$totalwords);
    $totalwords explode (" "$totalwords);
    $totalwords count ($totalwords);

                for(
    $x=0$x $count$x++)
                    {
                    
    $tally++;
                    
    $result .= $totalwords[$x] . " ";    
            if(
    $tally == 65 )
        {
                    
    $result .= "\n" $data[$di++];
            if(
    $di>=count($data))$di=0;
                    
    $tally 0;} 
                    
    $totalcount += $count;}

                     echo 
    $result;
                 
    ?>

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you echoed your value in $count? It does not have the correct value.
    And i think you have to do like this:
    PHP Code:
    $count count ($totalwords); 
    Your previous value is 0 due to this code:
    PHP Code:
    $count $oldcount $totalcount $tally 0
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for reply rajug;

    may be I should not have that, I had it when I use this script as pulling text out of db and then print it on page after data has been added which it does works.
    now I am trying to change some of the script and use it as pulling text out of a text file, add some data, but instead of echo the result write it to a text file,
    anyway I have replaced it with your code, same thing, no error, white page.

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you want to randomly to write or you know the location where to write the data read from CSV? Why do you want to count words? Do you want to write the whole content read from CSV or some portion only?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is not very complicated, the script should count words in text file,
    when it reaches a certain digit, then it takes a piece of data from
    csv file and insert it into the text file right after the finished count,
    the script keep counting, if there is more words keep doing the same thing until the end of text file.

  10. #10
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So do you mean inserting text (that is read from csv) will be the same repeatedly or that will be different like different lines of the CSV? And you mean that everything is working fine but the writing the final result is not working for you, right?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, there are different data in csv and every time one will be inserted into text file.

    I use a script that pulls text out of a DB, insert some data into it then publish it to a page. now when I say it is working I mean the original script,
    now I have changed little bit of script to pull text out of a text file instead of DB, and want to have the result in a text file instead of just echo the result on page.
    either in new text file or the same text file that read the text from.

    I have tried many variation, I am not good in php, so it did not work,
    I came here to get some help.


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
  •