/    Sign up×
Community /Pin to ProfileBookmark

Deleting specific txt doc file lines….

Can you delete/overwrite a specific line on a text document with PHP? I know you can overwrite a whole document, but I just want to take out or overwrite a specific line. Is that possible? It would make my data folder a lot less convoluted.

Thanx for any help.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bokehFeb 27.2006 — [code=php]<?php

// replace line 5
$textfile = 'textfile.text';

//Read file
$lines = @file($textfile) or die('I could not open '.$textfile);

$newtext = 'This is the new line 5'."n";

$lines[4] = $newtext; // keys start at 0... key 4 = line 5

// write file
$f = fopen($textfile, 'w') or die('I could not open '.$textfile);
fwrite($f, implode('', $lines));
fclose($f);

?>[/code]
×

Success!

Help @urquanmaster spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...