From text file to string

Posted on 2005-04-09
Last Modified: 2008-02-01

I have text file, elements.txt contents like:


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.

Question by:sam85281
    LVL 4

    Expert Comment

    Hi sam85281,

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


    $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

    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

    LVL 11

    Expert Comment

    How 'bout this?

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

    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()-->
    which returns the entire file as a string.

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

    Accepted Solution

    One more thing ;)
    You can use str_replace to strip out the newline characters:

    <?php $string = str_replace("\r\n",'',implode(',',file('elements.txt')));?>
    LVL 7

    Expert Comment

    <? $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.

    Expert Comment

    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,
    LVL 11

    Expert Comment

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

    Expert Comment

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


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now