用逗号替换给定文本中的新行字符


replacing new line characters with comma from given text

这里我尝试用逗号替换换行字符。我检查了以前的线程并采取了相应的行动,但仍然没有解决方案。

$letters = '#'s+#';
$rep   = ',';
$output  = str_replace($letters, $rep, trim($text));
echo $output;

链接到演示:http://ideone.com/DoFOSc

str_replace()用于字符串替换,而不用于正则表达式替换。如果你想做一个基于正则表达式的替换,应该使用preg_replace()。但是,您可以用逗号替换每一个新行,使用:

$output = str_replace(array("'n", "'r'n"), ",", $text);

using regex

preg_replace('|'n|', ',', $text)