0

I have a text file that looks like:

Line one
Line two
Line three

I would like to convert it to:

return array(
  'Line one',
  'Line two',
  'Line three'
);

And save it as a new file. I don't want to perform the same explode/preg_split over and over, that really doesn't make sense. I simply want to create a static file from the original file, and update it when I update the original file.

How would I go about doing this?

3
  • Explain why it doesnt make sense to use preg_split or a similar function? Seeing your question, it seems like the easiest way to handle this. Commented Jul 28, 2012 at 21:16
  • Sorry if I was not clear, I meant it does not make sense to perform it each time the script is needed, when I can make the changes once and not waste processing time at each call. Commented Jul 28, 2012 at 21:26
  • 1
    You can perform a simple check on both files to see if your input file has been modified after the time you last wrote your output file. If yes, write the output file with the info from the input file, if not, just use the current output file. This shouldnt give a significant performance loss at all. Commented Jul 28, 2012 at 21:29

3 Answers 3

2

Use the file function to read in the file:

$array = file('file1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

And to output it, use the file_put_contents and implode:

file_put_contents('file2.txt', implode("\n", $array));
Sign up to request clarification or add additional context in comments.

2 Comments

That's not what the OP asked. Read carefully.
@xtraorange: Either I'm confused, or you're confused.
2
$array = file($thefile,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
file_put_contents($theotherfile, "return array(\n  ".implode(",\n  ", $array)."\n);";

Comments

1

You can use var_export() to generate the required output.

Example:

$array = array();
for ($i = 0; $i < 10; $i++) {
    $array[md5($i)] = sha1($i);
}

echo var_export($array);

Result:

array (
  'cfcd208495d565ef66e7dff9f98764da' => 'b6589fc6ab0dc82cf12099d1c2d40ab994e8410c',
  'c4ca4238a0b923820dcc509a6f75849b' => '356a192b7913b04c54574d18c28d46e6395428ab',
  'c81e728d9d4c2f636f067f89cc14862c' => 'da4b9237bacccdf19c0760cab7aec4a8359010b0',
  'eccbc87e4b5ce2fe28308fd9f2a7baf3' => '77de68daecd823babbb58edb1c8e14d7106e83bb',
  'a87ff679a2f3e71d9181a67b7542122c' => '1b6453892473a467d07372d45eb05abc2031647a',
  'e4da3b7fbbce2345d7772b0674a318d5' => 'ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4',
  '1679091c5a880faf6fb5e6087eb1b2dc' => 'c1dfd96eea8cc2b62785275bca38ac261256e278',
  '8f14e45fceea167a5a36dedd4bea2543' => '902ba3cda1883801594b6e1b452790cc53948fda',
  'c9f0f895fb98ab9159f51fd0297e236d' => 'fe5dbbcea5ce7e2988b8c69bcfdfde8904aabc1f',
  '45c48cce2e2d7fbdea1afc51c7c6ad26' => '0ade7c2cf97f75d009975f4d720d1fa6c19f4897',
)

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.