Programming Basics

  Home arrow Programming Basics arrow Page 9 - Working with text files
PROGRAMMING BASICS

Working with text files
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2004-01-06

    Table of Contents:
  • Working with text files
  • Opening a text file
  • Writing data into a text file
  • Closing a text file
  • Reading from a text file
  • Deleting a file
  • Other functions regarding text files
  • An example
  • Advanced examples
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Working with text files - Advanced examples


    (Page 9 of 10 )

    Update selected rows in a text file
    <?php
    $filename = "data.txt"; // File which holds all data
    $rowToUpdate = 1; // This is line need to be updated
    $newString = "This text is updated\n"; // This is what you want to replace it with

    $arrFp = file( $filename ); // Open the data file as an array
    // Replace the current element in array which needs to be updated with new string
    $arrFp[$rowToUpdate-1] = $newString; 
    $numLines = count( $arrFp ); // Count the elements in the array

    $fp = fopen( $filename, "w" ); // Open the file for writing
    for($i=0; $i<$numLines; $i++) // Overwrite the existing content
    {
        fwrite($fp, $arrFp[$i]);
    }
    fclose( $fp ); // Close the file
    ?>

    In this example, first, the script opens a text file as an array. Then it replaces the array element which is relative to the row you want to update with the new string. Finally, the script again writes the elements of the array (text file lines) to the same text file, replacing the existing content. After closing the file, you could check your text file and you’ll notice that the line you wanted to update has been replaced by the value stored in $newString.

    Delete selected rows in a text file
    <?php
    $filename = "data.txt"; // File which holds all data
    $rowToDelete = 1; // This is line need to be deleted

    $arrFp = file( $filename ); // Open the data file as an array
    $numLines = count( $arrFp ); // Count the elements in the array

    $fp = fopen( $filename, "w" ); // Open the file for writing
    for($i=0; $i<$numLines; $i++) // Overwrite the content except the line to be deleted
    {
        if($i != ($rowToDelete-1) )
        fwrite($fp, $arrFp[$i]);
    }
    fclose( $fp ); // Close the file
    ?>

    Here you’ll find a way to delete a selected line from a text file using PHP codes. As usual, this opens a text file into an array with the use of file() function which I noted as a “very important function when it comes to file reading”. I hope you’ll see it in these examples. After making an array of lines, the script simply runs a loop of the array as it writes the elements into the same text file, except the line which you wanted to delete. Simple as that!

    Searching for restricted words
    <?php
    $filename = "data.txt"; // File which holds all data
    $inputString = "This is where you need to put some string which has bad words.";

    $arrFp = file( $filename ); // Open the data file as an array
    $numLines = count( $arrFp ); // Count the elements in the array

    $arrWords = explode( ' ', $inputString ); // Split the input string into words as an array
    $numWords = count( $arrWords ); // Count the words in the string

    for($i=0; $i<$numWords; $i++) // Loop through the words in the string
    {
        for($j=0; $j<$numLines; $j++) // Loop through the lines of the text file
        {
            if(strstr($arrWords[$i], trim( $arrFp[$j] ))) // Search whether the current words is restricted
                $arrWords[$i] = "*****"; // If it is replace the word with asterisks
        }
        
        $outputString .= $arrWords[$i].' ';
    }
    echo $outputString; // Echo the string replacing restricted words

    ?>

    This is a common situation which many of the webmasters have to face when some type of posting can be done in their sites. You should restrict words which is not suitable to be displayed in your site. You should store your restricted words list in a simple text file, line by line to start with. Then open the text file as an array. At the same time, split your string which includes the bad words into separate words. Write two loops one inside the other. First one is to loop through the words in your posted string and the second loops through the array which has the restricted words. While running the loop check whether the current word is a restricted word, replace the current word with few asterisk signs. Finally, when you echo the output it will show the posted string, with restricted words in asterisk marks. Handsome code!

    So, I hope you went through above examples. They will be very important and useful pieces of codes when you create applications.

    More Programming Basics Articles
    More By Codewalkers

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap