0

I am trying to get a file that looks like this:

Name1

Name2

Name3

and want it to output like so:

Name1, Name2, Name3

I tried this but am getting nowhere with it:

    $list = file_get_contents("tready.txt");
$convert = explode("\n", $list);
for ($i=0;$i<count($convert);$i++)  
{
    $list = $convert[$i].', '; //write value by index
}
$this->say('Currently waiting: ' . $list);
}

4 Answers 4

1

Something like that?

$list = file("tready.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
echo implode(", ", $list);

Docs: file, implode

Sign up to request clarification or add additional context in comments.

Comments

1

Easier:

$list = implode(', ', file('tready.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));

Comments

0

You'll achieve that simply with that code:

$list = file_get_contents("tready.txt");
$convert = implode(', ', explode("\n", $list));

What's happening here: you merge elements of an array (exploded $list) with , string.

Comments

0

Even easier:

echo str_replace("\n", ', ', file_get_contents("tready.txt"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.