开发者

How to: overwrite defined array with new POSTED data

开发者 https://www.devze.com 2023-02-11 07:10 出处:网络
I\'ll redefine my old unanswered & messy Question \'cause i\'m begging for Your help. Anyone... I have a line inside a file (choices.php) that has:

I'll redefine my old unanswered & messy Question 'cause i'm begging for Your help. Anyone...

I have a line inside a file (choices.php) that has:

some c开发者_运维技巧hoice1...||other choice2...||some data here...||OVERWRITE ME! I AM OLD!

On an other page there is a button that POST (and store/save) new data into choices.php. This NewData have to overwrite some text ($arrkey[3])

I tried explode / implode, file_put_contents, anything I've found browsing for answers. But the far I could get was the NewData being just ADDED at the end of that line:

some choice1...||other choice2...||some data here...||OVERWRITE ME! I AM OLD!I'M THE NEW DATA!

instead of:

some choice1...||other choice2...||some data here...||I'M THE NEW DATA!


If I got your question right, the following code will do what you want :

$entries = explode('||', file_get_contents('choices.php'));
$entries[3] = $_POST['myfield']; // You may want to do more strict checking before that.
file_put_contents('choices.php', implode('||', $entries));

And, by the way, choices.php is a bad name, since the file doesn't actually contain any PHP code. But it's won't prevent the script from working.

0

精彩评论

暂无评论...
验证码 换一张
取 消