Jump to content

Replace in .txt


Readme

Recommended Posts

My English is not good but I try to explain.

 

Now i want to use array function... can be used for multiple lines.

This:

$new_contents = str_replace("My name is", "is name my", $contents);
Something like this:
$contents= array(
            array(My name is, is name my),
            array(Hi,Hello),
            array(Come back, I coming));

<?php
$file = 'textfile.txt';

if (file_exists($file)) {

    $ff = fopen($file, 'r+');
    $contents = file_get_contents($file, FILE_TEXT);
    $new_contents = str_replace("My name is", "is name my", $contents);
    file_put_contents($file, $new_contents, FILE_TEXT); 
    fclose($ff);

    $ff = fopen($file, 'r+');
    fwrite($ff, $new_contents);
    fclose($ff);

} else {

    echo "The file $filename does not exist";

}
?> 

Link to comment
https://forums.phpfreaks.com/topic/203951-replace-in-txt/#findComment-1068201
Share on other sites

<?php
$file = 'textfile.txt';

if (file_exists($file)) {

    $ff = fopen($file, 'r+');
    $contents = file_get_contents($file, FILE_TEXT);
$change= array("My name is", "Hi", "Come back");
$replace = array("is name my","Hello","I coming");
    $new_contents = str_replace($change, $replace, $contents);
    file_put_contents($file, $new_contents, FILE_TEXT); 
    fclose($ff);

    $ff = fopen($file, 'r+');
    fwrite($ff, $new_contents);
    fclose($ff);

} else {

    echo "The file $filename does not exist";

}
?> 

 

something like this?

Link to comment
https://forums.phpfreaks.com/topic/203951-replace-in-txt/#findComment-1068212
Share on other sites

No, mraza.

 

Example!

 

textfile.txt - words

I coming

The file filename does not exist

Tired of these ads?

 

Not work but sample
$replace = array("I coming","1 2 3");
$replace = array("The file filename does not exist","1 2 3 4");
$replace = array("Tired of these ads?","1 2 3 4 5");

 

textfile.txt - now looks like this

1 2 3

1 2 3 4

1 2 3 4 5

 

Link to comment
https://forums.phpfreaks.com/topic/203951-replace-in-txt/#findComment-1068243
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.