user2129623
user2129623

Reputation: 2257

replacing new line characters with comma from given text

Here I have tried to replace newline char with comma. I checked previous threads and acted accordingly but still no solution.

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

Link to demo : http://ideone.com/DoFOSc

Upvotes: 2

Views: 2266

Answers (2)

ztripez
ztripez

Reputation: 664

str_replace() is for string replace and not for regex replace. preg_replace() should be used if you want to do a regex-based replace. However, you could replace each new line with a comma using:

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

Upvotes: 3

Manjeet Sharma
Manjeet Sharma

Reputation: 174

using regex

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

Upvotes: 3

Related Questions