Find and replace in a file - php

I want to replace certain strings with another one in a text file (ex: \nH with ,H). Is there any way to that using PHP?

You could read the entire file in with file_get_contents(), perform a str_replace(), and output it back with file_put_contents().
Sample code:
<?php
$path_to_file = 'path/to/the/file';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace("\nH", ",H", $file_contents);
file_put_contents($path_to_file, $file_contents);
?>

There are several functions to read and write a file.
You can read the file’s content with file_get_contents, perform the replace with str_replace and put the modified data back with file_put_contents:
file_put_contents($file, str_replace("\nH", "H", file_get_contents($file)));

If you're on a Unix machine, you could also use sed via php's program execution functions.
Thus, you do not have to pipe all of the file's content through php and can use regular expressions. Could be faster.
If you're not into reading manpages, you can find an overview on Wikipedia.

file_get_contents() then str_replace() and put back the modified string with file_put_contents() (pretty much what Josh said)

Related

How i can delete some data-attributes with value in file-content?

I want to delete some data-attributes with they values inside the sourcecode of a file.
I load the file inside a variable
$filecontent = file_get_contents($pathtofile);
Now I want to delete some code, for example
delete data-myval="something123"
Important to know is, that the value "something123" is dynamically.
How I can delete all matches of data-myval="find_all"
I tried to do it with str_replace and explode failed, it was to complicated and produced too long of code.
Is there a easier way to do it?
Thanks a lot.
You can achieve this using preg_replace. This will allow you to match any regex expressions and remove the code.
Your regex expression will be (see regex101):
data-myval\=\"[^"]+\"
Using preg_replace:
$filecontent = file_get_contents($pathtofile);
$filecontent = preg_replace('/data-myval\=\"[^"]+\"/', '', $filecontent);
After this you will need to save the file using file_put_contents().
file_put_contents($pathtofile, $filecontent);

Is it possible to overwrite a certain part of a txt file in php?

Let's say we have a .txt file with the text "1234567890",
is it possible to change the 1 to a 9 so it says "9234567890" without touching the remaining numbers?
To clarify, it should only change a part of the text file and not rewrite the entire thing. I'm only allowed to use php and html.
This can be a one-liner, but I'm breaking it up so it makes more sense. Further, this would be faster using exec with sed or awk, but this is just PHP here.
$string = file_get_contents("yourfile");
$string = str_replace('1','9',$string);
file_put_contents("youfile", $string);

php .txt file updating with r+ mode

I have been searching much in google. It shows many ways but I didn't have the particular answer I've been looking for. I want to know how to edit a particular string of a line or a whole line of a .txt file via php using r+ mode.
One way is to read whole file, replace your string and write whole new string to file again... if you are working with small files. Here is example:
<?php
$file = 'filename.txt';
$fileContent = file_get_contents($file);
$replaced = str_replace("replace me", "replace with", $fileContent);
file_put_contents($file, $replaced);
?>

PHP: How to use str_replace?

My server has been hit with a nasty javascript iframe virus. The Trojan injects itself in to every index.php, index.html, & login.php files. The virus looks like <script>VirusCodeCrap</script>
Is there anyway I could use PHP's str_replace function to search my server and delete the virus? Would anyone know wher I could find some examples on how to do this?
Thanks,
Albert
If you are on a Unix server, sed is the best way to find and replace text in files.
If you must use PHP, the algorithm will be:
Read a file into a variable using file_get_contents()
$file_contents = file_get_contents( $filename );
Search for the replace the offending string
$file_contents = str_replace( $the_offending_text, $the_replacement_text, $file_contents);
Write $file_contents back to the file using file_put_contents:
file_put_contents( $file_contents );
str_replace() may be insufficient if the string is not precisely the same in every case. If there are variations in the offending string, you may need to use a regular expression to locate and remove them.
I would immediately delete all files from your server and re-upload clean copies from wherever your code repository is. I would also find where the code was exploited and patch that security hole.
Going about it by editing the virus out of your files seems like the long and hard way when you should have a backup of your files ( that haven't been on a live server ).

php replace a pattern

Suppose in a file there is a pattern as
sumthing.c: and
asdfg.c: and many more.. with *.c: pattern
How to replace this with the text yourinput and save the file using php
The pattern is *.c
thanks..
You can read the contents of the file into a PHP string using file_get_contents, do the *.c to yourinput replacement in the string and write it back to the file using file_put_contents:
$filename = '...'; // name of your input file.
$file = file_get_contents($filename) or die();
$replacement = '...'; // the yourinput thing you mention in the quesion
$file = preg_replace('/\b\w+\.c:/',$replacement,$file);
file_put_contents($file,$filename) or die();
You can use PHP's str_replace or str_replace ( in case its a regex pattern). CHeck the syntax of these two functions and replace the *.c with your input.
.c pattern should be something like /?(.c)$/
First open file and get it's content:
$content = file_get_contents($path_to_file);
Than modify the content:
$content = preg_replace('/.*\.c/', 'yourinput');
Finally save the result back to the file.
file_put_contents($path_to_file, $content);
Note: You may consider changing the regexp because this way it match the '.c' string and everything before it. Maybe '/[a-zA-Z]*\.c/' is what you want.

Categories