Jump to content

Replace in .txt


Readme

Recommended Posts

Takes the data from the file (.txt) and replace it - Saving changes?

I searched the forum but did not find  :(

 

Bad code and slow

<?php
$string  = 'My name is';
$patterns = 'My name is';
$replacements = 'is name my';
echo str_replace($patterns, $replacements, $string ); 
?> 

Link to comment
Share on other sites

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
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
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
Share on other sites

sorry but i am not clear what you are trying to say, last script i assumed you have one file named "textfile.txt" within same location you are running this script , you wants to read that file and find some words and replace them, if so that should work, other then not clear to me  :shrug:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.