File Manipulation

Hi everybody,
I have the following exercise :
Write a php script that reads a file containing letters and numbers. In the read file, replace all the numbers with # sign. Write the modified data into another file.
I have written a simple code that I thought it may work ,but it does not and I don’t undersand why .
Here is my code:

<?php`Preformatted text` $read = fopen("file.txt","r"); // to open and read the file`Preformatted text` indent preformatted text by 4 spaces fread($read,filesize("file.txt")); //to read the whole file size `Preformatted text` indent preformatted text by 4 spaces $pattern = file_get_contents("file.txt"); // to get the whole contents of the file`Preformatted text` indent preformatted text by 4 spaces echo str_replace($pattern, '#',$read); // trying to replace numbers in the file with # `Preformatted text` indent preformatted text by 4 spaces $write = fopen("file.txt","w+"); // trying to write the file with the changes made in echo str_replace `Preformatted text` indent preformatted text by 4 spaces $replacement= file_put_contents($write, $pattern); // putting the contents in the file `Preformatted text` indent preformatted text by 4 spaces echo $write ; //printing the file with the changes made `Preformatted text` indent preformatted text by 4 spaces ?> `Preformatted text`
indent preformatted text by 4 spaces

I should do it with preg_replace , but it does not work at all.(or maybe I just do not know how to make it work :sweat_smile:)
Thank you for your time guys :blush:

p.s I don’t know how to edit the text so it looks just like I write it . why it is shown as a paragraph :confused:Preformatted text

<?php $f= 'C:\xampp\htdocs\ex\fiile.txt'; $conten = file_get_contents($f); $conten = str_replace("5","#",$conten); file_put_contents($f,$conten); echo $f; ?>

previously I tried this code , it works but it doesn’t match my requirments :confused:

When posting code, please use the ‘Preformatted Text’ menu button </>.

Programming is not some random process where you keep trying different things until you eventually get one to work. It is an exact science. The computer only does exactly what your code and data tells it to do. All the php statements are documented as to what input parameters they take, what processing they do, and what value they return. The first posted code looks like a random jumble of statements that don’t have anything to do with using the php documentation. Return values are not used, return values that have a specific type are used as parameters in other functions that don’t take those type of values, multiple statements that perform the same action are being used.

Next, to produce code that does what you want, first define what inputs you have/need, what processing you are going to do on those inputs, and what result or output you are going to produce. You then break each part of the process down into the steps needed to accomplish the process, taking into account limits and extents, such as will all the data in the input file fit in memory at once… You then write, test, and debug the code for each step, before going onto the next step.

You already have a statement of the top level process, break this down into numbered steps -

  1. Read a file - will this data all fit into memory or will you need to loop over each line in the file?
  2. Replace all numbers with a #.
  3. Write the data to another (different) file. I’m pretty sure that everyone here thinks that writing the data to another file means it is in addition to, separate from, different from the input file.

Your 2nd posted code is much better than the 1st. It implements the steps, except that it only replaces the number ‘5’ in the data. The only complaint about the 2nd code is don’t use one letter variables. Variables should be named as to the meaning of the data in them. How about using $in_file for the input filename and $out_file for the output file name?

So, you are left with finding a way of replacing all numbers (without writing out line after line of code for each possible value.) Your course work/instructor shouldn’t have given you this assignment without first covering at least one method of accomplishing this task. However, when you were examining the documentation for str_replace(), one of the possible values for the search parameter is an array. Specifically -

If search is an array and replace is a string, then this replacement string is used for every value of search.

So, at this point you are faced with producing an array containing all the numbers (again, hopefully, without writing out all the values.) I recommend that you look at the range() function to do this part of this step.

1 Like

One way to avoid making people like phdr angry is to format your code properly and add comments to the code indicating what you think each part of the code is doing. It helps other people understand what the code is doing and that you actually have some understanding of what you’re doing and it helps us tailor our response to your skill level. Also, 90% of the time just adding comments will help you to understand your problem better and you might not even need to ask any question here.

I used to spend a large part of my day writing very detailed forum posts asking for help. Often by the time I had completed what I considered to be a high quality forum post I had also understood my problem so well that I did not need to ask the question anymore.

https://blog.codinghorror.com/rubber-duck-problem-solving/

1 Like

Thank you for your reply and your time . I appreciate it . I will consider your advice and hope I will solve it. I am not so used to programimng and its logic yet so please forgive me.

Thank you. you are right .I do not know how to edit text here that’s why my code looks like that .I will improve my writing .

Hey guys I worked on it and did it with a simple code :slight_smile: I am commenting it here incase someone else needs it :slight_smile:

  1. List item
<?php` indent preformatted text by 4 spaces $file_o = file_get_contents("file.txt"); //Open and read the given file indent preformatted text by 4 spaces echo $file_o; //print the unedit file indent preformatted text by 4 spaces echo "
"; //to create space between files,original and edit `Preformatted text` $file_o = preg_replace("([0-9]+)","#",$file_o); //to replace the numbers on the file with '# `Preformatted text` print $file_o; //print the edit file ?>

p.s: I tried to edit just like you guys said with </> , but I guess it is not working and I am sorry. But I hope my code helps somebody :slight_smile:

<?php
$file_o = file_get_contents("file.txt"); 
//Open and read the given file 
echo $file_o; 
//print the unedit file
echo " "; 
//to create space between files,original and edit
$file_o = preg_replace("([0-9]+)","#",$file_o); 
//to replace the numbers on the file with '# 
print $file_o; 
//print the edit file
?> 

This is a regular line of text.

This is four spaces and then the line of text.

This is another regular line of text.

    This is 8 spaces and then a line of text. 

The first 4 spaces cause the code formating to happen. The next 4 spaces are included in the code formatted text you see above.

Your code editor should have some kind of functionality that allows automatically adding 4 spaces to every line of text in a file.

If you know regular expressions you can use find and replace in most editors to find all end of line characters and replace them with end of line + 4 spaces - this should cause all lines to start with 4 spaces.

You can then copy that into the phphelp website 
and get code formatting like this.
Sponsor our Newsletter | Privacy Policy | Terms of Service