Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace certain content in txt file and save it using PHP?
    text
    copied!<p>So I receive variable replace it in certain area in txt file and save it back. I get page number and according to it I get exploded data. Anyway I'll post a code below to make it more clear: </p> <pre><code>$pgnm = $_GET['page']; //This is the page number as I've said. $conts = file_get_contents("content.txt"); </code></pre> <p>the content of content.txt looks like this:</p> <pre><code>text1|text2|text3 </code></pre> <p>I display this content in certain pages. For example on first page: text1, on second text2, etc.</p> <p>Now i'm working on a form where I successfully change these. I get as I've said page number and text:</p> <pre><code>$text = "new text"; //this is the content which I want to be replaced instead of text2. </code></pre> <p>I make the content.txt file look like this after its saved: text1|new text|text2</p> <p>So lets go on:</p> <pre><code>$exp = explode("|", $conts); //this explodes data into slashes. $rep = str_replace($exp[$pgnm], $text, $conts); file_put_contents("content.txt", $rep); // Saving file </code></pre> <p>All these above-mentioned operations work perfectly, but here's my problem now. This only works if content.txt has certain content, if it's empty it enters my new text and that's all: 'new text' and that's all. Maybe I want to add second page content 'new text2' and after I finish entering it and save I want the file to be displayed like this: new text|new text2. If the content of content.txt looks like this: 'new text|' str_replace doesn't replace empty string. So that's my another problem too.</p> <p>I tried everything, but couldn't manage to anything about this two problems. Thank you in advance for your help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload