/    Sign up×
Community /Pin to ProfileBookmark

Writing a string..

How can I insert a string in between two lines of a file if I know what lines I need to insert the string between?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@nebulousAug 13.2004 — hm...be more precise ?
Copy linkTweet thisAlerts:
@shimonAug 13.2004 — Seems clear enough to me - neenach has a text file of a certain amount of lines - let's say 20 lines, and wants to insert some text between line 10 and line 11.

This is a long-winded way to do it but I can't think of a better way just now.

[code=php]<?php

// Let's say this is the text to insert
$insert_text = 'Hi! I am some new text!';

// And we want to insert it between ln 10 and 11
$insert_after = 10;

// The filename
$filenm = 'myfile.txt';

// Read in the contents of the text file
// (you may wish to make a backup first)
$contents = file_get_contents($filenm);

// Split contents into an array
$contents = explode("n", $contents);

$new_contents = array();

$ln_num = 1;
while (list(, $val) = each($contents)) {
if ($ln_num == 10) {
$new_contents[] = $insert_text;
$ln_num++;
}
$new_contents[] = $val;
$ln_num++;
}

// print the new contents back to file
$new_contents = join('', $new_contents);
file_put_contents($filenm, $new_contents);

?>[/code]


It's untested, and there must be a better way...but it's a start ?
Copy linkTweet thisAlerts:
@solavarAug 13.2004 — [i]Originally posted by shimon [/i]

It's untested, and there must be a better way...but it's a start ? [/QUOTE]


It's a great start.

I can't think of a better way except maybe to use file() instead of file_get_contents.

Since file() grabs the contents already as an array, it is just a matter of inserting the new text at the desired line_number.
Copy linkTweet thisAlerts:
@shimonAug 13.2004 — doh! well spotted ? In earlier PHP versions I got so used to having to work *around* the fact that file() returns an array, that I overlooked it when I needed it!

I also realised that file_put_contents() won't work before PHP5, but some combination of fopen(), fwrite() and fclose() will do the same job.
Copy linkTweet thisAlerts:
@neenach2002authorAug 13.2004 — Now I want the line to be written after the last similar line. The line is similar to this: [CODE][link=Site domain]Site name[/link][/CODE]

If I can't do it this way, then I can always insert the data before the first line that looks like that, as that line number will always hold a similar value.

The whole

http://hosting.kingdombattles.comp/hosting/signup.php
Copy linkTweet thisAlerts:
@neenach2002authorAug 13.2004 — Alright....The code is now working...sort of...
[code=php]// Let's say this is the text to insert
$insert_text = '[link=' . $_POST['domain'] . ']' . $_POST['sname'] . '[/link]';

// And we want to insert it between ln 20 and 21
$insert_after = 20;

// The filename
$filenm = '../e107_plugins/custom/Hosted.php';

// Read in the contents of the text file
// (you may wish to make a backup first)
$contents = file_get_contents($filenm);

// Split contents into an array
$contents = explode("n", $contents);

$new_contents = array();

$ln_num = 1;
while (list(, $val) = each($contents)) {
if ($ln_num == 10) {
$new_contents[] = $insert_text;
$ln_num++;
}
$new_contents[] = $val;
$ln_num++;
}

// print the new contents back to file
$new_contents = join('', $new_contents);
$fpoint = fopen($filenm, 'w');

fwrite($fpoint , $new_contents);
fclose($fpoint)[/code]

My only problem is it doesn't write the file how I need it to. The file is rendered through the e107 CMS, which is why I need to write the previous contents back to the file, as well as keeping the 'Hosted sites' list intact. I want to add a site to the list every time someone applies for hosting.

The file that the above code spits out can be found [URL=http://hosting.kingdombattles.com/e107_plugins/custom/hosted1.txt]here[/URL]

I have uploaded what the file should look like.

[upl-file uuid=05446e27-2b8e-4757-82c8-c780e82153c8 size=964B]hosted.txt[/upl-file]
Copy linkTweet thisAlerts:
@neenach2002authorAug 17.2004 — Any answers anyone?
×

Success!

Help @neenach2002 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.18,
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,
)...