• Welcome to Valhalla Legends Archive.
 

[PHP] Deleting charecters from a text file

Started by Black4C6F747573, December 04, 2004, 08:20 PM

Previous topic - Next topic

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.

Banana fanna fo fanna

- Read the file into a buffer
- Make the appropriate changes
- Overwrite file with new data

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

x=0;
while(x<4){
fwrite($fp,NULL);
x++;
}



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

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:


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


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

EpicOfTimeWasted

Quote from: Black4C6F747573 on December 05, 2004, 09:25 PM
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

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

$string = implode("", file("file.name"));

Do you want to strip out a specific substring?

$string = str_replace("substring to strip goes here", "", $string);

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

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

You'd then obviously write $string back to the file.

Black4C6F747573

Quote from: Arta[vL] on December 06, 2004, 05:01 AM
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:


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


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

This would turn

asdf asdf bob asdf asdf

to asdf asdf asdf asdff, how would I get rid of the last charecter?