用换行符替换逗号并写入文本文件[关闭]

用换行符替换逗号并写入文本文件[关闭]

问题描述:

I want to take value on the database and replace the comma with a newline and then write to a text file.

Example:

SELECT column FROM table WHERE ID = 6
Result: Monday,Tuesday,Wednesday,Thursday,Friday

I want it to then remove the commas and write to a .txt file with newline.

Result:

Monday

Tuesday

Wednesday

Thursday

Friday

Hope this makes sense.

我想在数据库上取值并用换行符替换逗号,然后写入文本文件。 / p>

示例: p>

  SELECT列FROM表WHERE ID = 6 
Result:星期一,星期二,星期三,星期四,星期五
   code>  pre> 
 
 

我希望它删除逗号并用换行符写入.txt文件。 p>

结果: p>

 星期一
 
星期二
 
星期三
 
星期五
 
星期五
  code>  pre> 
 
 

希望这是有道理的。 p > div>

Use something like

file_put_contents("file.txt", str_replace(",", "
", $string));

File_put_contents saves the file file.txt. http://php.net/manual/en/function.file-put-contents.php
Str_replace replace the commas with new line. http://php.net/manual/en/function.str-replace.php

File_put_contents("file.txt", str_replace(",", PHP_EOL, $string));

assuming you know how to execute your query and fetch the result:

it basically comes to the function str_replace. (could also be replaced in the query using mysql's REPLACE() function)

<?php
  file_put_contents('/myfile.txt', str_replace(',', PHP_EOL, $row['column']));

But your question is quit limited in information.

btw: should the file not be overwritten: Use FILE_APPEND as 3rd parameter of file_put_contents