By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,275 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,275 IT Pros & Developers. It's quick & easy.

modifying a text file

bonski
P: 53
i've been trying to modify some lines from a text file... and i had this text file and a php script to modify it...

//code.txt
Expand|Select|Wrap|Line Numbers
  1. 101:2001
  2. 102:2002
  3. 103:2003
  4. 104:2004
//code_reader.php
[PHP]<?php

$code_file = 'code.txt';
$code_file_lines = file($code_file);

if(isset($_GET['address']) && isset($_GET['code'])){

$address = $_GET['address'];
$code = $_GET['code'];

$c = 0;
for($a = 0; $a<sizeof($code_file_lines); $a++){
$content = explode(":", $code_file_lines[$a]);
if($address == $content[0] && $code != $content[1]){
$code_content[$c] = trim($content[0]).':'.trim($code);
}else{
$code_content[$c] = trim($content[0]).':'.trim($content[1]);
}
$c++;
}

$new_code = implode('\n', $code_content);
file_put_contents($code_file, $new_code);
}

?>
[/PHP]

now if i try to send to send values like this to modify a certain line...

Expand|Select|Wrap|Line Numbers
  1. code_reader.php?address=102&code=2001
i want to output it as this...

Expand|Select|Wrap|Line Numbers
  1. 101:2001
  2. 102:2001
  3. 103:2003
  4. 104:2004
but it turns out like this...

Expand|Select|Wrap|Line Numbers
  1. 101:2001\n102:2001\n103:2003\n104:2004
i know... using

[PHP]file_put_contents($code_file, $new_code);[/PHP]

will write it as string.. is there any function that will convert it to stream so that when it modifies the text file.. it can read "\n".


thanks..

bonski
Jun 25 '07 #1
Share this Question
Share on Google+
3 Replies


bonski
P: 53
hahaha... nevermind guys... i already found a solution.. using.. [PHP]chr(10)[/PHP] instead of '\n'.
Jun 25 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya, bonski.

PHP won't parse '\n' when it's enclosed in single quotes. You need to use "\n" instead.
Jun 25 '07 #3

bonski
P: 53
Heya, bonski.

PHP won't parse '\n' when it's enclosed in single quotes. You need to use "\n" instead.
ei pbmods,

ok.. thanks for the info... gotta try that now... hahaha..

^___^ bonski..
Jun 27 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.