473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

modifying a text file

bonski
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
3 1246
bonski
53
hahaha... nevermind guys... i already found a solution.. using.. [PHP]chr(10)[/PHP] instead of '\n'.
Jun 25 '07 #2
pbmods
5,821 Expert 4TB
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
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

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

Similar topics

33
by: Jason Heyes | last post by:
I would like to modify the contents of a file, replacing all occurances of one string with another. I wrote these functions: bool read_file(std::string name, std::string &s); bool...
5
by: jrmsmo | last post by:
Hi I have a document as follows: <?xml version="1.0"?> <metadata xml:lang="en"> </metadata> I want to change the document so it looks as follows: <?xml version="1.0"?> <metadata...
1
by: Max Khitrov | last post by:
Hello everyone, I'm working on a VS .NET add-in that will allow developers to use Subversion software from within the IDE (much like Source Safe). Ideally, I would like for my plug-in to be able...
8
by: vadim | last post by:
Hi, Is there a .Net control available that allows to write into web.config file appsettings section? The idea is to create encrypted user name and password for database connection and then...
2
by: Randall Powell | last post by:
I am in the process of developing a Windows Service which will: (1) monitor multiple network shares; (2) marshal text file transfers into an SQL Server 2000 instance; and (3) provide messaging...
2
by: rk | last post by:
I have the following library.xml file coming from a system, this can't be modified. ____________________________________________________________________________ <?xml version="1.0"...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
1
ganesanji
by: ganesanji | last post by:
hi to all, I am new to php. I have to edit a text file using php. I saw the file system concepts modes. My problem is I want to change a particular text or word in a text file. How to...
1
by: Joe Cool | last post by:
I am attempting to add a function to an application I am working on to modify the JPEG Comment in a Jpeg image file. I can retrieve the JPEG Comment with no problem. The problem is modifying it....
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.