Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

From text file to string

Posted on 2005-04-09
7
Medium Priority
?
233 Views
Last Modified: 2008-02-01
Hello,

I have text file, elements.txt contents like:

pressKit
pressPhoto
soundSample
biography

I need to generate from that list a variable like:
$elements = "pressKit,pressPhoto,soundSample,biography";

I'm pretty sure I need to use array code of some kind, but still haven't gotten around to figuring out the whole array thing yet.  Also not really sure on how to convert from a one element per line format, to a comma seperated format.

Thanks!
-Sam
0
Comment
Question by:sam85281
7 Comments
 
LVL 4

Expert Comment

by:danny_ebbers
ID: 13742802
Hi sam85281,

You could read them in a comma seperated string or in a array i will provide you both samples below

<?php
$elements_string='';
$elements_array=array();

$handle = fopen("/path/to/elements.txt", "r"); // Opens the file itself for READING access see the r
while (!feof($handle)) {   // opens a loop until feof(File End Of Feed) ,
   $buffer = fgets($handle, 4096); // Reads until next line or until 4096 bytes are recieved.
   $elements_array[]= $buffer;               // adds buffer to array elements
}
fclose($handle);

var_export($elements_array);  // shows the array itself on the screen
$elements_string = implode(',',$elements_array); //  pastes the elements of the array together using , as seperator
echo $elements_string; // shows the elements as string
?>

Cheers!
0
 
LVL 11

Expert Comment

by:matt_mcswain
ID: 13742899
How 'bout this?

<?php $string = implode(',',file('elements.txt'));?>

file()-->http://us3.php.net/file
does most of the work for you by returning an array with each line as a separate value of the array.
Also look at file_get_contents()-->http://us3.php.net/manual/en/function.file-get-contents.php
which returns the entire file as a string.

php has tons of file and string manipulation functions which make these tasks very easy.
0
 
LVL 11

Accepted Solution

by:
matt_mcswain earned 2000 total points
ID: 13742955
One more thing ;)
You can use str_replace to strip out the newline characters:

<?php $string = str_replace("\r\n",'',implode(',',file('elements.txt')));?>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:Promethyl
ID: 13744101
<? $d=str_replace(' ',',',file_get_contents('elements.txt')); ?>

Pretty easy stuff. This is where having a database is nice. Or you could serialize an array, and write that to disk. That's always an option.
0
 

Expert Comment

by:shai126
ID: 13754357
the previous comments are far more complicated than need be. just do something like this:

<?

$elements_array = file("elements.txt"); // gets each line onto a different element of the array
$elements_string = implode(",", $elements_array); // put all elements into a string, with a ',' (comma) seperator

?>

Hope this helps,
Shai
0
 
LVL 11

Expert Comment

by:matt_mcswain
ID: 13760085
>> the previous comments are far more complicated than need be. just do something like this:
Did you read the previous comments? :)
0
 
LVL 7

Expert Comment

by:Promethyl
ID: 13761407
Matt: Agreed. We've jumped through tremenedous hoops to make something simple even easier.

0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question