What is cross-platform regular expression for string replacement?

I have a type string "foo\nbar", but depending on the platform it may become "foo\n\rbar"or something else. I want to replace newlines with ", ". Is there a good (php) regex that will do this for me?

+5
source share
3 answers

Try regex (?:\r\n|[\r\n]):

preg_replace('/(?:\r\n|[\r\n])/', ', ', $str)
+5
source

You do not want to use Regex for a simple replacement like this. Regular string replacement functions are usually much faster. To break a line, you can use a constant that supports the OSPHP_EOL , for example

str_replace(PHP_EOL, ', ', $someString);

Windows \r\n. Mac \r \n.

+2

str_replace(array("\n", "\r"), "", $string)?

-1

All Articles