用给定文本中的昏迷替换新行字符

时间:2014-06-02 13:31:56

标签: php

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

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

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

使用正则表达式

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