Deleting lines of text from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BOMEz
    New Member
    • Dec 2007
    • 40

    #1

    Deleting lines of text from a file

    Hi all,

    I'm just trying to delete some lines of text from a text file that I open with fopen.

    Based on search results I've tried using
    Code:
    str_replace($_POST['mySearchString'], "", fgets($openedFile));
    This gives no result
    I've also tried it as :

    Code:
    str_replace($_POST['mySearchString'], "", $openedFile);
    Where am I going wrong?
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    #2
    Question how are you open the file:
    are you making it writing? is the file locked?

    nomad

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      What are you doing with the return value of the function?

      The str_replace function, like most of the string functions, doesn't alter the variables you pass into it, but rather uses those variables to create a new one, which it returns.

      So, if you did:
      [code=php]
      $text = "I like oranges.";
      $output = str_replace("or anges", "apples", $text);
      echo $text;
      [/code]
      It would still output "I like oranges."

      The altered version "I like apples" would be in the $output variable.

      Comment

      • BOMEz
        New Member
        • Dec 2007
        • 40

        #4
        Originally posted by Atli
        What are you doing with the return value of the function?

        The str_replace function, like most of the string functions, doesn't alter the variables you pass into it, but rather uses those variables to create a new one, which it returns.

        So, if you did:
        [code=php]
        $text = "I like oranges.";
        $output = str_replace("or anges", "apples", $text);
        echo $text;
        [/code]
        It would still output "I like oranges."

        The altered version "I like apples" would be in the $output variable.
        I don't need a return value, I'm just trying to get some lines of text out of a text file. For example if I have :

        Cat1
        Cat2
        Cat3
        Cat4
        Cat5
        and I want to get rid of Cat 3 from the text file replacing it with:

        Cat1
        Cat2
        Cat4
        Cat5
        additionally I am opening the file with "r+" permissions.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by BOMEz
          I don't need a return value, I'm just trying to get some lines of text out of a text file.
          Whether or not you need a return value is irrelevant.

          Like I explained in my previous post, the str_replace function returns the altered string. It does not alter the strings you pass into it.
          Therefore, if you want to change a string, you need to use the return value of the str_replace function, or the call is completely pointless.

          In your case, however, the str_replace function isn't that useful.
          I would just use the fgets function to copy a file line by line, skipping the lines that you want excluded.

          Comment

          Working...