Valhalla Legends Forums Archive | Web Development | [PHP] Deleting charecters from a text file

AuthorMessageTime
Black4C6F747573
I am having some issues deleting charecters from a text file.  I dont want to delete a line mind you, for which i could just skip the array element, but certain charecters.  I know it sounds pointless but it will benefit my script :-)

I was thinking fwrite over the top, but would null work? is it just a null charecter? I know '' wouldnt work because it would just not write anything... Any feed back on this matter would be greatly appreciated.
December 5, 2004, 2:20 AM
St0rm.iD
- Read the file into a buffer
- Make the appropriate changes
- Overwrite file with new data
December 5, 2004, 7:30 PM
Black4C6F747573
Well, my problem is this, say I have

johnžasdfasdfasdfasdf|aaa|123|aa|456|
and I want to remove 123|, but i don't want to replace it with blank spaces,  or put any information in it's place. what would the code to delete it look like? I thought maybe
[code]
x=0;
while(x<4){
fwrite($fp,NULL);
x++;
}

[/code]

but would that work? is NULL just a null charecter that wont display in a text file, but will overwrite things? or what about fputc(127); to place a delete charecter in there, would that delete forwards or backwards? or maybe
fputc(08); in the for loop with the pointer after the 123|, would that use the control charecter backspace 4 times? just things im wondering.  ty for any input :-P
December 6, 2004, 3:25 AM
Arta
No.

If you delete elements (characters, in this case) from the middle of an array (which is what a string really is) you need to move the elements appearing after the deleted ones back, to fill up the space. In pseudocode:

[code]
for(start of string to end of string)
  if(the current character is the one you're deleting)
  begin
      for(current position to end of string - 1)
          string[current position] = string[current position -1]
   
      break
  end
[/code]

There's a bit of extra work to do if you're deleting a substring, but I'll let you figure that out :)
December 6, 2004, 11:01 AM
EpicOfTimeWasted
[quote author=Black4C6F747573 link=topic=9783.msg91193#msg91193 date=1102303504]
Well, my problem is this, say I have

johnžasdfasdfasdfasdf|aaa|123|aa|456|
and I want to remove 123|, but i don't want to replace it with blank spaces,  or put any information in it's place. what would the code to delete it look like? I thought maybe
[/quote]

Since the goal is to strip the characters from a file, you'd want to start off with something like:

[code]$string = implode("", file("file.name"));[/code]

Do you want to strip out a specific substring?

[code]$string = str_replace("substring to strip goes here", "", $string);[/code]

Do you want to strip out certain characters, but not a specific substring?

[code]$string = ereg_replace("[characters you want to strip go here]", "", $string);[/code]

You'd then obviously write $string back to the file.
December 8, 2004, 12:22 AM
Black4C6F747573
[quote author=Arta[vL] link=topic=9783.msg91213#msg91213 date=1102330913]
No.

If you delete elements (characters, in this case) from the middle of an array (which is what a string really is) you need to move the elements appearing after the deleted ones back, to fill up the space. In pseudocode:

[code]
for(start of string to end of string)
   if(the current character is the one you're deleting)
   begin
      for(current position to end of string - 1)
          string[current position] = string[current position -1]
     
      break
   end
[/code]

There's a bit of extra work to do if you're deleting a substring, but I'll let you figure that out :)
[/quote]

This would turn

asdf asdf bob asdf asdf

to asdf asdf asdf asdff, how would I get rid of the last charecter?
December 9, 2004, 3:03 PM

Search