0
我有這樣值的.txt文件的列表:PHP正則表達式question..adding逗號
aaa
bbbb
ddd
eeeee
我怎麼會附加一個逗號和對所有這些結束,這樣一個空間列表中查找此
aaa,
bbbb,
ddd,
eeeee,
感謝
我有這樣值的.txt文件的列表:PHP正則表達式question..adding逗號
aaa
bbbb
ddd
eeeee
我怎麼會附加一個逗號和對所有這些結束,這樣一個空間列表中查找此
aaa,
bbbb,
ddd,
eeeee,
感謝
無需正則表達式等。只是get所有從你的文件中的文本,做一個好醇」 str_replace()
和put回:
$contents = file_get_contents("myfile.txt");
$contents = str_replace("\n", ",\n", $contents);
file_put_contents("myfile.txt", $contents);
這不會插入逗號,如果最後一行沒有一個換行符,但如果你真的需要它在那裏,這裏是一個改進版本,與交易:
$contents = trim(file_get_contents("myfile.txt"));
$contents = str_replace("\n", ",\n", $contents) . ",";
file_put_contents("myfile.txt", $contents);
str_replace ("\n", ",\n", $your_variable);
str_replace
將足夠多的滿足您的需求
爲什麼要費心正則表達式?只需將該文件逐行讀入一個字符串中,再附加一個逗號和一個空格,然後依次將每行寫回。 – Amber 2011-03-13 05:09:42