Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

implode() query

Status
Not open for further replies.

ckennerdale

Programmer
Dec 23, 2000
158
GB
I am trying to write an array in to string which is then written to a text file

I am using implode() on the array and using fputs this string is written to my text file-
so far so good

However in my text file there is a line break after each entry,which i need to get rid of

(This is not /r or /n but a pysicalline break)

As this file is be readmy another application my string needsto written all on 1line ofthe text file-

$cat1text is the array

$cat1text = implode(",",$cat1text);
$fp = fopen("cat1text.txt", "w");
fputs($fp,$cat1text);
fclose($fp); Caspar Kennerdale
Senior Media Developer
 
need a little more info...

are you using the file as a "text-file-based database" ?
if so, are you basically adding just 1 record, or multiple records... one on each line?

if it is as a table, why r u not using sql instead of a text file?

if it is just 1 record in that file, just read in the record, take the length of the string (which includes the hard linefeed) and trim off the last character...

$a=$record
$b=strlen($record)
$b--;
$output=substr($a,0,$b);

I am assuming that the file is only gonna be holding 1 record (since you are using mode "w" which opens file for writing and puts pointer at beginning of line so as to overwrite anything that is there...)

another thing u could try is to use ereg_replace to convert the carriage return to nothing...
go to php.net and lookup the ereg_replace command. it has lots of examples on how to clean out /r /n and other versions of carraige returns as well...
 
Yes it needs to write over the file every time, which it is doing succesfully,

The text file will then be imported via an app developed in Director

The issue is that when written to the text file each element on the array is written to a different line in the text file. There is no /n or /r.

The string all needs to on one line so as not to cause any errors in director.

The 'string' looks like this-

item1
,item2
,item3
,item4
,item5

and I need it to look like this-

item1...,item2,item3,item4,item5

Caspar Kennerdale
Senior Media Developer
 
Best option is I think to walk trough the array, stripping all \r\n and then implode... But its just a guess, I havent tried it. mcvdmvs
-- "It never hurts to help" -- Eek the Cat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top