Find a specific line and replace it with a new one? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a .php file that contains array data like below:
$config['setting_one'] = true;
$config['setting_two'] = "some string";
$config['setting_three']['option1'] = "some string";
What is the correct way to find the line starts with $config['setting_two'] and replace the line with $config['setting_two'] = "new string"; (new string will come from a post value).

There is no correct way.
The correct way is "don't do that".
You include the php code, and then update the variable by simply setting it.
What are you trying to do?

You will have to read the file line by line using fgets
$config = array();
while(($line = fgets($handle, 4096) ) !== false) {
$config[] = $line;
}

Related

Content of File PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a file that i opened:
$linie = file("hasla-kopia.txt"); (It's in Polish)
And i have a problem with getting content of a file where every element is in the next line.
This is how my file looks like:
enter image description here
I want the exact output on my php file. I know you can do it by nl2br() function but it's really important for me to make every element in a different HTML tag because i want my final output to be:
1000111011111 in decimal system is: some number
and I don't know how to do the content on separate lines, change to int and write this text after it. Can someone please help me out?
$lines = file('hasla-kopia.txt');
foreach ($lines as $line_num => $line) {
echo $line."<br>";
}
that allows reading line by line and each line is the variable $line
you can use
$file = file_get_contents("hasla-kopia.txt");
$em = explode("\n" , $file);
for($i=0;$i<count($em);$i++){
echo $em[$i];
};

PHP - Trying to delete some elements of an array then repost all the content to a file. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
if(isset($_POST["delete"])){
$file_name = 'ids';
touch($file_name);
$handle = fopen($file_name, 'r+');
$stream = file_get_contents('ids');
$pieces = explode(" ", $stream);
unset($pieces[0]);
unset($pieces[1]);
unset($pieces[2]);
file_put_contents("ids", "\n" );
file_put_contents("ids", $pieces);
fclose($handle);
}
So I'm sure this is wicked messy and a terrible way to accomplish my goal but I am learning PHP for school and this is giving me a lot of issues.
When I press my button I want to take the content I have in 'ids', change it by deleting the strings located in the array at 0,1,2 and then rewrite the new content to 'ids'. Any help would be appreciated.
I'm not sure where you are getting stuck, but I would guess it is regarding the "re-assembly" process, so I'll fix that.
Untested Code:
if(isset($_POST["delete"])){
$file_name='ids';
touch($file_name);
$handle=fopen($file_name, 'r+');
$stream=file_get_contents($file_name);
$content=str_replace(" ","\n",explode(" ", $stream,4)[3]); // explode into a max of 4 elements, access the last element, replace spaces with `\n`
file_put_contents($file_name,$content);
fclose($handle);
}

PHP: Replace php code in string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want replace php codes in string and running fro server
$x = 1;
$string = "OK:{first}Yes{second}No";
I want replace if($x == 1){ to {first} and } else { to {second}
After run and echo $string , I want result in html :
OK:Yes
How did you come up with this approach? Why not simply:
$string = $x == 1 ? 'OK:Yes' : 'OK:No';
That would get you the string you want based on the value of $x.
Also if you literally mean that you want to do a search-n-replace on the PHP code itself (as #OllyTenerife assumes), then are you going to write it into a file to be executed later, or use eval() or something? Doesn't sound like the right track there... In which case, remodel your code.
$string = str_replace("{first}","if($x == 1)",$string);
$string = str_replace("{second}","} else {",$string);

How to retrieve a random line from text file, then remove the line from the text file in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am having a bit of a hard time thinking about what to do for this. I would like to display a random line from a specific text file, then after it displays it will remove that line from the text file so it will no longer show up when page refreshed.
$lines = file('');
echo $lines[array_rand($lines)];
file read lines into array. array_rand to get a random key from the array of lines. unset to remove an element. file_put_contents to write back to file.
$filename = 'filename.txt';
$lines = file($filename);
$r = array_rand($lines);
echo $lines[$r];
unset($lines[$r]);
file_put_contents($filename, implode("", $lines));

File Writing in php without appending [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Following is my piece of code which writes to file.
<?php
$fileWrite = fopen("c.txt", "w+");
for($i=0;$i<5;$i++) {
$bytes = fwrite($fileWrite, $i);
}
fclose($fileWrite);
I am getting 01234. It means , pointer is appending to last location, I don't want to append data. Instead need to write 4 in the file.
Then you need to ftruncate the file before writing to it:
ftruncate($fileWrite, 0);
$bytes = fwrite($fileWrite, $i);
This is obviously pretty pointless to do in a loop, but I expect you know that.
I personally recommend using file_put_contents for this simple task. It's easier to use and will not append to the end of the file (unless specified that way).

Categories