Programming Basics
  Home arrow Programming Basics arrow Page 9 - Working with text files
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PROGRAMMING BASICS

Working with text files
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · Well laid steps in this tutorial. Thank you.
       · You have done a great job here.Thanks
       · no crap... good tutorial
       · This is the text.
       · Thanks, a very helpful tutorial
       · Really, I built a whole portal engine in 4 days with this tutorial. You can check it...
       · Your guide is very interesting, useful and good.Can I translate it in Romanian...
     

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek